Is it a good idea to declare a class in if condition block ? I will need to do this in Magento. Condition came up even if a module is installed but has a setting which tells condition is false then it should not rewrite parent methods, otherwise it should rewrite parent method. I will need to include this check in all class declaration of whole module I will build. Let me know if this is a good idea.
PHP Code:
if($condition):
// this class rewrites parent methods
class myClass extends myParentClass{
public function myMethod(){
return 'ok';
}
}
else:
// this class doesn't rewrite parent methods
class myClass extends myAnotherParentClass{}
endif;
This should really be solved with polymorphism and a better inheritance structure. If you are 'rewriting' parent methods in your class, is it probably a better idea to instead use an abstract superclass (or an interface) and then extend variants of myClass from it. You can then generate the appropriate instance based on your condition (probably using some implementation of the factory pattern to keep client code clean), and as far as any client code is concerned it has an instance of myClass to work with - it doesn't need to know or care about the specifics of how the particular instance operates.
The first rule of Tautology Club is the first rule of Tautology Club.
It might also be a good candidate for the Decorator pattern, using the condition to decide at run-time which "decorator" to use with the main object that contains it.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Thanks buddies ! but I think you guys are not getting what I really want to do...
It is certain that I would have to have "class myClass extends myParentClass" anyhow... I just don't want to write something like this in each methods of class I write (becoz in sense of resource usage and coding itself)
PHP Code:
class myClass extends myParentClass{
public function myMethod(){
if($condition) return parent::myMethod();
//do my stuffs
}
public function myMethodAgain(){
if($condition) return parent::myMethodAgain();
//do my stuffs
}
}
So wanted to know if declaring classes inside such "if" blocks is a good idea or is there any other better idea ??? But the Decorator Pattern wouldn't do for me... (-_-)..... orrrrr I could have not grabbed good idea of how it works.... but I would really need to use the methods of parent those I also might need to override or just use as it is... but I guess I would have to write something like this for all methods when using Decorator technique huh NogDog ?
PHP Code:
class myClass{
protected parentObj;
protected function __construct(myParentClass $parentObject){
$this->parentObj = $parentObject;
}
public function myThirdMethod(){
//I would have to do like this in all methods from parent I would need to use just as they were, right ?
return $this->parentObj->myThirdmethod();
}
...
}
You could use the "magic" __call() method along with call_user_func_array():
PHP Code:
<?php
/* top decorator */
class ParentClass
{
public function myMethod()
{
return "Default behavior";
}
public function neverChanges()
{
return "This does not change";
}
}
/* child decorator */
class ChildClass extends ParentClass
{
public function myMethod()
{
return "New behavior";
}
}
/* main class that uses the decorator */
class MyClass
{
protected $decorator;
public function __construct($condition)
{
$this->decorator = ($condition) ? new ParentClass() : new ChildClass();
}
public function __call($name, $args = array())
{
return call_user_func_array(array($this->decorator, $name), $args);
}
public function notDecorated()
{
return "I'm not decorated";
}
}
// SAMPLE USAGE
foreach(array(true, false) as $condition)
{
$obj = new MyClass($condition);
printf(
"<p>%s. %s. %s.</p>\n",
$obj->myMethod(),
$obj->neverChanges(),
$obj->notDecorated()
);
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks