note 105146 added to language.oop5.decon
| From: | WrinkledCheese at osu1 dot php dot net | Date: | Sat, 30 Jul 2011 07:52:22 +0000 |
| Subject: | note 105146 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182014@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 216.118.145.154
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/105146
Del: integrated -- https://master.php.net/note/delete/105146/integrated
Del: useless -- https://master.php.net/note/delete/105146/useless
Del: bad code -- https://master.php.net/note/delete/105146/bad+code
Del: spam -- https://master.php.net/note/delete/105146/spam
Del: non-english -- https://master.php.net/note/delete/105146/non-english
Del: in docs -- https://master.php.net/note/delete/105146/in+docs
Del: other reasons-- https://master.php.net/note/delete/105146
Reject -- https://master.php.net/note/reject/105146
Search -- https://master.php.net/manage/user-notes.php