Page 1 of 1
PHP - Static functions
Posted: Tue Aug 16, 2011 7:20 pm
by sg00
Given:
Code: Select all
class MyClass
{
public static function get()
{
return 'OK';
}
}
$c = new MyClass();
And considering we only have access to $c and not the class name (so cant do MyClass::get()), which of the following is correct? (btw: all work)
Code: Select all
// #1
echo($c::get());
// #2
echo($c->get());
// #3
$class = get_class($c);
echo($class::get()); // Only in PHP 5.3
Re: PHP - Static functions
Posted: Tue Aug 16, 2011 8:48 pm
by yacahuma
is this a test? For me is correct is they work, if they all work, then they are all correct. As to which one will I use #2
Re: PHP - Static functions
Posted: Tue Aug 16, 2011 9:19 pm
by sg00
Hello, Thankyou for the reply. It's not a test. I really cant figure out which is most correct. I don't want to implement something that is deprected in a future PHP version and I can't find a definitive answer on php.net.
Re: PHP - Static functions
Posted: Tue Aug 16, 2011 9:28 pm
by yacahuma
what I dont undesrtand is why are you saying you dont have access to the class name??
Re: PHP - Static functions
Posted: Tue Aug 16, 2011 9:53 pm
by sg00
Here is an exmple:
Code: Select all
interface Feedable
{
public static function getName();
}
class Horse implements Feedable
{
public static function getName()
{
return 'Horse';
}
}
class AnimalFarm
{
public function feed(Feedable $animal)
{
echo('<pre>');
// #1?
echo('Feeding: '.$animal::getName()."\n");
// OR #2?
echo('Feeding: '.$animal->getName()."\n");
// OR #3?
$class = get_class($animal);
echo('Feeding: '.$class::getName()."\n"); // Only in PHP 5.3
}
}
$farm = new AnimalFarm();
$farm->feed(new Horse());
Re: PHP - Static functions
Posted: Tue Aug 16, 2011 10:18 pm
by yacahuma
I dont agree with your example. If this is the actual code I will use abstract classes instead
Code: Select all
abstract class AnimalFarm
{
protected $name;
function getName()
{
return $this->name;
}
abstract function feed();
}
class Horse extends AnimalFarm
{
function __construct()
{
$this->name = 'Horse';
}
public function feed()
{
echo('Feeding: '.$this->getName()."\n");
}
}
class Cow extends AnimalFarm
{
function __construct()
{
$this->name = 'Cow';
}
public function feed()
{
echo('Feeding: '.$this->getName()."\n");
}
}
$horse = new Horse();
$horse->feed();
$cow = new Cow();
$cow->feed();
Re: PHP - Static functions
Posted: Wed Aug 17, 2011 12:45 am
by sg00
Thankyou very much for the reply. I understand your point of view, however I think we will have to agree to disagree with each others examples. In the case of inheritance (ie: "extends") the child should form an "is a" relationship with the parent class. "A Cow is an AnimalFarm" is not correct and shows the inheritance model is incorrect. "A Cow is an Animal" would be correct, however in my case I am designing an AnimalFarm rather than an Animal.
"An AnimalFarm can feed animals that are feedable" is more correct, which is why I needed to use the interface.
For the record, these terms are simply analogies for other more complicated things I am building. I'm not actualy building an animal farm
