tags:

views:

77

answers:

5

Up until the yesterday I though that both of these methods for accessing a class where identical. Google turned up noting (either that or my keywords were bad). What is the difference between accessing a class by defining a new instance of the class rather than just calling the class methods directly.

I hardly understood what I just ask, since I have not a clue what I'm asking but allow me to give a few examples to help out.

For as long as I have been using classes in PHP I have been defining a instance of the class then accessing the methods from that variable. Here is an example:

<?php
class Myclass
{
   public function my_method() {
      return "This is my method"; 
   }
}
$myclass = new Myclass();

echo $myclass->my_method(); // output: This is my method
?>

What is the difference between that and doing this?

<?php
class Myclass
{
   public function my_method() {
      return "This is my method";
   }
}

echo Myclass::my_method(); // output: This is my method
?>

I hope I was clear enough. If not a brief synopsis would be - What is the difference between creating a new instance of a class then calling a method of the class with "$class->mymethod();," or just directly calling the method like so: "Myclass::mymethod();"?

Thanks in advanced.

+3  A: 

The :: notation is used to call the function as static. See these pages from the PHP manual for more info:

In essence, static functions are just like regular functions outside classes. The function is not in object context when called as such, so the pseudo-variable $this is not available.

Note that the function should be declared as static if you are planning on calling it as such:

class SomeClass {

    static public function StaticFunction() {
        ...
    }

}
Tatu Ulmanen
+2  A: 

The Myclass::my_method() call should be throwing an E_STRICT warning, as you're calling a non-static method in a static context.

Static methods are methods that belong to the class rather than to the instance. These methods cannot access any non-static variables, as they have no reference for them (because non-static variables belong to the instance, not the class).

After using Java and C#, I'm mildly disturbed that this isn't considered an E_WARNING or E_ERROR, particularly since E_STRICT is not logged by default.

R. Bemrose
A: 

http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

I don't think you can call methods that way. I thought that was how you called static members of the class.

MrChrister
A: 

In my understanding when you create an instance of the class you are creating an object and so you call methods/properties from that object.

When you call a method/property via a static call you are calling on the class itself.

So if you have something like this:

class acs {
    public $var = 'bla';        
}

and then you do

$a = new acs();
$b = new acs();

echo $a->var . '<br>';
echo $b->var . '<br>';

$a->var = 'hey';
$b->var = 'Yooo';

echo $a->var . '<br>';
echo $b->var . '<br>';

You now have 2 instances of class acs which started with the same values but now have different values.

But if you have this:

class acs {
    public static $var = 'bla';     
}

And then do:

$a = new acs();
$b = new acs();

echo $a->var . '<br>';
echo $b->var . '<br>';


$a->var = 'hey';
$b->var = 'Yooo';

echo $a->var . '<br>';
echo $b->var . '<br>';

echo acs::$var;

What you are doing is:

  • You create the instances as before.

  • You echo nothing, because the public property var does not exist.

  • You create the public property var in both the instances

  • You echo their values (hey and Yoo)

  • You echo the static property var (which is 'bla' and has not changed)

Hope this helps!

AntonioCS
A: 

First, you should understand the difference between a class and an object, or a class and an instance of a class.

For example, where class is "Circle", an instance might be "this particular circle". Class Circle might have members $center and $radius - every point has these. But the values of these are different for every point.

$class->mymethod() is a call to an instance method, for instance $circle->Length(). You are saying something to a particular instance of an object. This method will have access to class members - variables that belong to the instance. In PHP you gain access to these instance variables using keyword $this.

For instance $circle->Length() might be implemented as return 2*Pi*$this->radius;

What happens when you say Circle::Length()? Nothing. This is syntax for calling static methods. Because you did not say which circle. Circle as a class does not have length.

Static method is declared with keyword static. Whatever it does it has no access to any instances of the class. It's something that a class as a whole does.

For instance, if you want to calculate length of the circle without actually creating one, then there might be static function calculateLength($radius) in the class circle. And you would call it as Circle::calculateLength($myRadius)

In my opinion you should not even think about static methods until you understand the non-static ones.

yu_sha
Good explanation, it is still a blur even after reading all of the answers and some documentation on php.net. I assume this is just one of those things that I don't understand right now, and all of a sudden I'll get an epiphany and call myself stupid later.
David