note 78009 added to language.oop5.static
| From: | nospam dot dd at dyce dot com | Date: | Mon, 24 Sep 2007 07:49:39 +0000 |
| Subject: | note 78009 added to language.oop5.static | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130736@lists.php.net to get a copy of this message | ||
Doh!
Yes of course my last example works, but it doesn't actually solve the problem of self being
assigned in the superclass for static functions!!! Idiot.
Yes, you can only do it by passing the name of the subclass. In case, like me you've been
struggling with it for a while, here's one way to do it, using a separate
'instatiate' method to do the generic leg-work for __construct in the superclass, whilst
using a stub __construct method in all the subclasses to ensure that the correct static method gets
called,
<?php
class domainobject {
protected $properties; //!< associative array of domainobject properties
public function __construct($new_object_properties = null) {
$classname = 'domainobject';
$this->instantiate($classname);
}
protected function instantiate($classname) {
$this->properties = call_user_func(array($classname,'property_setup'));
}
static function property_setup() {
$properties['id'] = array('type' => 'int', 'size'
=> '10');
return $properties;
}
}
class person_object extends domainobject {
public function __construct($new_object_properties = null) {
$classname = 'person_object';
$this->instantiate($classname);
}
static function property_setup() {
$properties['forename'] = array('type' => 'text',
'size' => '10', 'validations' => 'required');
$properties['surname'] = array('type' => 'text',
'size' => '10', 'validations' => 'required');
return $properties;
}
}
class phone_object extends domainobject {
public function __construct($new_object_properties = null) {
$classname = 'phone_object';
$this->instantiate($classname);
}
static function property_setup() {
$properties['phone_label'] = array('type' => 'text',
'size' => '10');
$properties['number'] = array('type' => 'text',
'size' => '10');
return $properties;
}
}
print_r(new phone_object());
print_r(new person_object());
/* Result:
phone_object Object (
[properties:protected] => Array
(
[phone_label] => Array ( [type] => text, [size] => 10 ),
[number] => Array ( [type] => text, [size] => 10 )
)
person_object Object (
[properties:protected] => Array
(
[forename] => Array ( [type] => text, [size] => 10, [validations] =>
required ),
[surname] => Array ( [type] => text, [size] => 10, [validations] =>
required )
)
) */
?>
----
Server IP: 194.145.210.3
Probable Submitter: 81.132.14.36
----
Manual Page -- http://www.php.net/manual/en/language.oop5.static.php
Edit -- https://master.php.net/note/edit/78009
Del: integrated -- https://master.php.net/note/delete/78009/integrated
Del: useless -- https://master.php.net/note/delete/78009/useless
Del: bad code -- https://master.php.net/note/delete/78009/bad+code
Del: spam -- https://master.php.net/note/delete/78009/spam
Del: non-english -- https://master.php.net/note/delete/78009/non-english
Del: in docs -- https://master.php.net/note/delete/78009/in+docs
Del: other reasons-- https://master.php.net/note/delete/78009
Reject -- https://master.php.net/note/reject/78009
Search -- https://master.php.net/manage/user-notes.php