note 78009 deleted from language.oop5.static by felipe
| From: | felipe@php.net | Date: | Mon, 24 Mar 2008 23:14:08 +0000 |
| Subject: | note 78009 deleted from language.oop5.static by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137519@lists.php.net to get a copy of this message | ||
Note Submitter: nospam dot dd at dyce dot com
----
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 )
)
) */
?>