Skip to content

Commit

Permalink
A word about static vs non static calls; fixes #41 (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Mar 7, 2017
1 parent 01f50e2 commit ddf3978
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions source/codingstandards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ parent class ``parent::theMethod()``
another class ``ClassName::theMethod()``
================= ===========

Static or Non static?
^^^^^^^^^^^^^^^^^^^^^

Some methods in the source code as `declared as static <http://php.net/manual/fr/language.oop5.static.php>`_; some are not.

For sure, you cannot make static calls on a non static method. In order to call such a method, you will have to get an object instance, and the call the method on it:

.. code-block:: php
<?php
$object = new MyObject();
$object->nonStaticMethod();
It may be different calling static classes. In that case; you can either:

* call statically the method from the object; like ``MyObject::staticMethod()``,
* call statically the method from an object instance; like ``$object::staticMethod()``,
* call non statically the method from an object instance; like ``$object->staticMethod()``.
* use `late static building <http://php.net/manual/en/language.oop5.late-static-bindings.php>`_; like ``static::staticMethod()``.

When you do not have any object instance yet; the first solution is probably the best one. No need to instanciate an object to just call a static method from it.

On the other hand; if you already have an object instance; you should better use any of the solution but the late static binding. That way; you will save performances since this way to go do have a cost.

Classes
-------

Expand Down

0 comments on commit ddf3978

Please sign in to comment.