I understand that there are two ways to access a PHP class - "::" and "->". Sometime one seems to work for me, while the other doesn't, and I don't understand why.
What are the benefits of each, and what is the right situation to use either?
I understand that there are two ways to access a PHP class - "::" and "->". Sometime one seems to work for me, while the other doesn't, and I don't understand why.
What are the benefits of each, and what is the right situation to use either?
The "::" symbol is for accessing methods / properties of an object that have been declared with the static keyword, "->" is for accessing the methods / properties of an object that represent instance methods / properties.
Simply put, ::
is for class-level properties, and ->
is for object-level properties.
If the property belongs to the class, use ::
If the property belongs to an instance of the class, use ->
class Tester
{
public $foo;
const BLAH;
public static function bar(){}
}
$t = new Tester;
$t->foo;
Tester::bar();
Tester::BLAH;
When you declare a class, it is by default 'static'. You can access any method in that class using the ::
operator, and in any scope. This means if I create a lib class, I can access it wherever I want and it doesn't need to be globaled:
class lib
{
static function foo()
{
echo "hi";
}
}
lib::foo(); // prints hi
Now, when you create an instance of this class by using the new
keyword, you use ->
to access methods and values, because you are referring to that specific instance of the class. You can think of ->
as inside of
. (Note, you must remove the static keyword) IE:
class lib
{
function foo()
{
echo "hi";
}
}
$class = new lib;
$class->foo(); // I am accessing the foo() method INSIDE of the $class instance of lib.
It should also be noted that every static function can also be called using an instance of the class but not the other way around.
So this works:
class Foo
{
public static function bar(){}
}
$f = new Foo();
$f->bar(); //works
Foo::bar(); //works
And this doesn't:
class Foo
{
protected $test="fddf";
public function bar(){ echo $this->test; }
}
$f = new Foo();
$f->bar(); //works
Foo::bar(); //fails because $this->test can't be accessed from a static call
Of course you should restrict yourself to calling static methods in a static way, because instantiating an instance not only costs memory but also doesn't make much sense.
This explanation was mainly to illustrate why it worked for you some of the times.
Sourcing WikiPedia - Class
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint describes the state and behavior that the created objects all share. An object created by a class is an instance of the class, and the class that created that instance can be considered as the type of that object, e.g. a type of an object created by a "Fruit" class would be "Fruit".
The ::
operator accesses class methods and properties which are defined in php using the static
keyword. Class const
are also accessed using ::
The ->
operator accesses methods and properties of an Instance of the class.
If the function operates on an instance, you'll be using ->
. If it operates on the class itself, you'll be using ::
Another use of ::
would be when you want to call your parent
functions. If one class inherits another - it can override methods from the parent class, then call them using parent::function()
Php can be confusing in this regard you should read this.
What's also confusing is that you can call non static functions with the :: symbol. This is very strange when you come from Java. And it certainly surprised me when I first saw it.
For example:
class Car
{
public $name = "Herbie <br/>";
public function drive()
{
echo "driving <br/>";
}
public static function gas()
{
echo "pedal to the metal<br/>";
}
}
Car::drive(); //will work
Car::gas(); //will work
$car = new Car();
$car->drive(); // will work
$car->gas(); //will work
echo $car->name; // will work
echo Car::$name; // wont work error
As you can see static is very loose in php. And you can call any function with both the -> and the :: symbols. But there is a difference when you call with :: there is no $this reference to an instance. See example #1 in the manual.