note 105146 deleted from language.oop5.decon by danbrown

From: Date: Mon, 01 Aug 2011 13:51:14 +0000
Subject: note 105146 deleted from language.oop5.decon by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182061@lists.php.net to get a copy of this message
Note Submitter: Wrinkled Cheese ---- misha at gmail dot com: [quote]This is kinda weird to me as it is not obvious behavior.... Initiating constructor having no arguments, using call with parameters will not throw error/warning. <?php class A { # no arguments constructor function __construct() { echo "WierdoO"; } } $param = 1; $a = new A( $param ); ?> // Output "WierdoO" Notice that this anomaly is observed only for No Argument Constructor case.[/quote] Try replacing echo "Wierdo0"; with echo func_num_args()...or better yet: class A{ public function __constructor(){ $args = func_get_args(); foreach($args as $key => $value){ echo $key . " => " . $value . "<br>"; } } } $a = new A(); $a = new A("one"); $a = new A("one", 2); $a = new A("one", 2, "0x11"); Think of it as a simple way to overload a function. You can do it to any function, not just constructors. http://www.php.net/manual/en/function.func-get-args.php http://www.php.net/manual/en/function.func-get-arg.php http://www.php.net/manual/en/function.func-num-args.php All three pages say this, or similar: This function may be used in conjunction with func_get_arg() and func_get_args() to allow user-defined functions to accept VARIABLE LENGTH ARGUMENT LISTS.

« previous php.notes (#182061) next »