Calling Parent Class Methods

gudy_16

New Member
Question edited to better reflect my needs.Take the following example:\[code\]class Base{ public $Text = null; public function __construct() { $this->Text = new Base_Text(); }}class Base_Text extends Base{ public $Is = null; public function __construct() { $this->Is = new Base_Text_Is(); } public function CammelCase($string) { return trim(str_replace(' ', '', ucwords($string))); }}class Base_Text_Is extends Base_Text{ public function CammelCase($string) { return ($string === $this->CammelCase($string)); // doesn't work }}\[/code\]How can I fix the \[code\]Base_Text_Is::CammelCase()\[/code\] method without calling the Base_Text class statically (not using \[code\]parent::\[/code\] or \[code\]Base_Text::\[/code\])?The only solution I came up with for this kind of situations is to create a singleton function like this:\[code\]function Base(){ static $instance = null; if (is_null($instance) === true) { $instance = new Base(); } return $instance;}\[/code\]And change the \[code\]Base_Text_Is::CammelCase()\[/code\] method to this:\[code\]return ($string === Base()->Text->CammelCase($string));\[/code\]And in order to avoid creating two object instances of the Base class, instead of doing:\[code\]$base = new Base();$base->Text->Is->CammelCase('MethodOverloading'); // true\[/code\]I just do:\[code\]Base()->Text->Is->CammelCase('MethodOverloading'); // true\[/code\]Is this a good practice? Are there any other solutions?
 
Back
Top