There are two distinct ways to access methods in PHP, but what's the difference?
$response->setParameter('foo', 'bar');
and
sfConfig::set('foo', 'bar');
I'm assuming ->
(dash with greater than sign or chevron) is used for functions for variables, and ::
(double colons) is used for functions for classes. Correct?
Is the =>
assignment operator only used to assign data within an array? Is this in contrast to the =
assignment operator which is used to instantiate or modify a variable?
::
is used in static context, ie. when some method or property is declared as static:Also, the
::
operator (the Scope Resolution Operator, a.k.a Paamayim Nekudotayim) is used in dynamic context when you invoke a method/property of a parent class:->
is used in dynamic context, ie. when you deal with some instance of some class:By the way: I don't think that using Symfony is a good idea when you don't have any OOP experience.
When the left part is an object instance, you use
->
. Otherwise, you use::
.This means that
->
is mostly used to access instance members (though it can also be used to access static members, such usage is discouraged), while::
is usually used to access static members (though in a few special cases, it's used to access instance members).In general,
::
is used for scope resolution, and it may have either a class name,parent
,self
, or (in PHP 5.3)static
to its left.parent
refers to the scope of the superclass of the class where it's used;self
refers to the scope of the class where it's used;static
refers to the "called scope" (see late static bindings).The rule is that a call with
::
is an instance call if and only if:$this
exists and$this
is either the class of the method being called or a subclass of it.Example:
Output:
The
=>
operator is used to assign key-value pairs in an associative array. For example:It's meaning is similar in the
foreach
statement:Actually by this symbol we can call a class method that is static and not be dependent on other initialization...
Here the doWrite() function is not dependent on any other method or variable, and it is a static method. That' why we can call this method by this operator without initializing the object of this class.
Test::doWrite('Mrinmoy'); // Output: Hello Mrinmoy.
But if you want to call the
write
method in this way, it will generate an error because it is dependent on initialization.The difference between static and instantiated methods and properties seem to be one of the biggest obstacles to those just starting out with OOP PHP in PHP 5.
The double colon operator (which is called the Paamayim Nekudotayim from Hebrew - trivia) is used when calling an object or property from a static context. This means an instance of the object has not been created yet.
The arrow operator, conversely, calls methods or properties that from a reference of an instance of the object.
Static methods can be especially useful in object models that are linked to a database for create and delete methods, since you can set the return value to the inserted table id and then use the constructor to instantiate the object by the row id.
Yes, I just hit my first
'PHP Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM'
. My bad, I had a$instance::method()
that should have been$instance->method()
. Silly me.The odd thing is that this still works just fine on my local machine (running PHP 5.3.8) - nothing, not even a warning with error_reporting = E_ALL - but not at all on the test server, there it just explodes with a syntax error and a white screen in the browser. Since PHP logging was turned off at the test machine, and the hosting company was too busy to turn it on, it was not too obvious.
So, word of warning: apparently, some PHP installations will let you use a $instance::method(), while others don't.
If anybody can expand on why that is, please do.