PHP - Static functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sg00
Forum Newbie
Posts: 4
Joined: Tue Aug 16, 2011 7:14 pm

PHP - Static functions

Post 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
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: PHP - Static functions

Post 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
sg00
Forum Newbie
Posts: 4
Joined: Tue Aug 16, 2011 7:14 pm

Re: PHP - Static functions

Post 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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: PHP - Static functions

Post by yacahuma »

what I dont undesrtand is why are you saying you dont have access to the class name??
sg00
Forum Newbie
Posts: 4
Joined: Tue Aug 16, 2011 7:14 pm

Re: PHP - Static functions

Post 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());
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: PHP - Static functions

Post 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();
sg00
Forum Newbie
Posts: 4
Joined: Tue Aug 16, 2011 7:14 pm

Re: PHP - Static functions

Post 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 :wink:
Post Reply