note 99007 added to language.oop5.decon
| From: | maramirezcWHEREstar-devDOTTEDcom at osu1 dot php dot net | Date: | Wed, 21 Jul 2010 21:14:23 +0000 |
| Subject: | note 99007 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171027@lists.php.net to get a copy of this message | ||
Another way to emulate multiple constructors:
<?php
class MyClass {
// use a unique id for each derived constructor,
// and use a null reference to an array,
// for optional parameters
function __construct($id="", $args=null) {
// parent constructor called first ALWAYS
parent::__construct();
echo "main constructor\n";
// avoid null or other types
$id = (string) $id;
// allow default constructor
if ($id == "") {
$id = "default";
}
// just in case, use lowercase id
$id = "__cons_" . strtolower($id);
$rc = new ReflectionClass("MyClass");
if (!$rc->hasMethod($id) {
echo "constructor $id not defined\n";
}
else {
// using "method references"
$this->$id($args);
}
$rc = null;
} // function __construct
// ALWAYS HAVE A default constructor,
// that ignores arguments
function __cons_default($args=null) {
echo "Default constructor\n";
}
// may have other constructors that expect different
// argument types or argument count
function __cons_min($args=null) {
if (! is_array($args) || count($args) != 2) {
echo "Error: Not enough parameters (Constructor min(int $a, int $b)) !!!\n";
}
else {
$a = (int) $args[0];
$b = (int) $args[1];
$c = min($a,$b);
echo "Constructor min(): Result = $c\n";
}
}
// may use string index (associative array),
// instead of integer index (standard array)
function __cons_fullname($args=null) {
if (! is_array($args) || count($args) < 2) {
echo "Error: Not enough parameters (Constructor fullname(string $firstname, string
$lastname)) !!!\n";
}
else {
$a = (string) $args["firstname"];
$b = (string) $args["lastname"];
$c = $a . " " . $b;
echo "Constructor fullname(): Result = $c\n";
}
}
} // class
// --> these two lines are the equivalent
$obj1 = new TestClass();
$obj2 = new TestClass("default");
// --> these two are specialized
// should be read as "$obj3 = new TestClass, min(99.7, 99.83);"
$obj3 = new TestClass("min", array(99.7, 99.83));
// should be read as "$obj4 = new TestClass, fullname("John", "Doe");"
$obj4 = new TestClass("fullname", array("firstname" => "John",
"lastname" => "Doe"));
// --> these two lack parameters
$obj5 = new TestClass("min", array());
$obj6 = new TestClass("fullname");
?>
----
Server IP: 69.147.83.197
Probable Submitter: 189.156.192.49
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/99007
Del: integrated -- https://master.php.net/note/delete/99007/integrated
Del: useless -- https://master.php.net/note/delete/99007/useless
Del: bad code -- https://master.php.net/note/delete/99007/bad+code
Del: spam -- https://master.php.net/note/delete/99007/spam
Del: non-english -- https://master.php.net/note/delete/99007/non-english
Del: in docs -- https://master.php.net/note/delete/99007/in+docs
Del: other reasons-- https://master.php.net/note/delete/99007
Reject -- https://master.php.net/note/reject/99007
Search -- https://master.php.net/manage/user-notes.php