-1

I am still learning OOP PHP and I keep swapping and changing between the following way of calling methods within an object

$obj = new Model();
$obj->method($param);

against

Model::method($params);

I understand the difference when I within the method as I can use $this in the first example, and I have to use self:: in the second.

Which is the correct way and what are the reasons of using each way

The reason I ask is I cannot find a suitable search term to research. I am currently reading a book on OOP and it will probably tell at some point, but would be nice to know now.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Liam
  • 536
  • 8
  • 23
  • `$this` and `self` are not the same. `$this` is the instance whereas `self` is the class. – Florent Nov 27 '13 at 14:51
  • 1
    Consider being able to use `::method` on non-static methods a feature rather than something you're supposed to ever do. – h2ooooooo Nov 27 '13 at 14:53

4 Answers4

2

Foo::bar() calls the static class method, while $foo->bar() calls the instance method on an object. These are two completely different things. You do not need an object instance to call Foo::bar(), and in fact you do not have access to instance data when doing so. Foo::bar() is essentially nothing else but a regular function call like bar(), except that the function is attached to a class.

Instance methods act on a specific object instance:

class User {

    protected $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public static function hi() {
        // no access to $this->name here, since static
        // methods are not bound to specific instances
        echo 'Hi';
    }

}

$dave = new User('Dave');
$mary = new User('Mary');

echo $dave->getName();  // Dave
echo $mary->getName();  // Mary

User::hi(); // Hi

Unless you understand this, you know nothing about OOP.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Yes I under stand the above, but wouldn't – Liam Nov 27 '13 at 14:56
  • No, it wouldn't. Uhm, what? ;-) – deceze Nov 27 '13 at 15:00
  • Sorry press enter ;) So if the method requires properties contained in the class then it cannot be called statically, an instance needs to be made – Liam Nov 27 '13 at 15:03
  • Yes. `static` methods are always acting on the same data when called, if they're acting on data at all. *Instance methods* have access to the specific data of the object they're called on. – deceze Nov 27 '13 at 15:05
  • 1
    This may or may not be a little advanced, but try reading this: [How Not To Kill Your Testability Using Statics](http://kunststube.net/static/) – deceze Nov 27 '13 at 15:06
  • Great reading thanks. One more Question if you dont mind class Test { protected static $var = array(1=>'t',2=>'i',3=>'p') } do i call like Test::$var[1]; – Liam Nov 27 '13 at 17:00
  • Ok found out on anther thread [This Link](http://stackoverflow.com/questions/1212171/dynamically-call-a-static-variable-array) – Liam Nov 27 '13 at 17:03
1

First example is a non-static call to the method, second a static call.
The first is better if you want to access private variables of your Model, second is better if you use the method like a normal function.
In general you should declare methods of the first type as static (public static function method(){}).

0

First case is invocation of method on class instance, second case is call of static method.

See http://php.net/manual/en/language.oop5.static.php

Kacer
  • 679
  • 3
  • 12
0

There is no "proper" way because both call types serve different purposes.

The first call type is the standard way of handling objects: You initialize a concrete instance of a class. This instance can have its own internal values and each instance can use these values to create a different result when you call the method with the same parameter.

The second call type is called static and operates directly on the class, there is no instance (hence no $this). There are some use cases for it, see this answer for Java, it's the same for PHP.

Community
  • 1
  • 1
chiborg
  • 26,978
  • 14
  • 97
  • 115