Code: Select all
class MyClass
{
public static function get()
{
return 'OK';
}
}
$c = new MyClass();
Code: Select all
// #1
echo($c::get());
// #2
echo($c->get());
// #3
$class = get_class($c);
echo($class::get()); // Only in PHP 5.3
Moderator: General Moderators
Code: Select all
class MyClass
{
public static function get()
{
return 'OK';
}
}
$c = new MyClass();
Code: Select all
// #1
echo($c::get());
// #2
echo($c->get());
// #3
$class = get_class($c);
echo($class::get()); // Only in PHP 5.3
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());
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();