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