This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
where we use object operator “->” in php
In PHP 5, what are the advantages of typing $class::method() instead of $class->method()?
As in any performance or functional differences. Or is this just a way of forcing code non-PHP4 friendly because of the complete rewrite?
In PHP5, the two aren't interchangeable.
Static method calls will perform faster than non-static calls (over many iterations) but then the method is called in the static context and there is no object available to the called method.
The only reason PHP lets you call a non-static method using the static notation was for backwards compatibility in PHP 4 (because PHP 4 didn't have the static modifier for functions, or public/protected/private). If you do call a non-static method statically, you get a warning about "Strict Standards" output, and eventually this may fail with a fatal error.
So the answer really is to call the method the way it was supposed to be called. If it is a static method in PHP 5, then call it statically Class::method(), if it is a public method, then call it using the object $class->method().
Consider this code (run in PHP 5):
class Foo {
protected $bar = 'bar';
function f() {
echo $this->bar;
}
}
echo Foo::f(); // Fatal error: Using $this when not in object context
$class::method() calls a static method of the class whereas $class->method() calls a public standard method of the class.
Related
This question already has answers here:
PHP: Static and non Static functions and Objects
(5 answers)
Closed 8 years ago.
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.
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.
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(){}).
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
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.
Why is the 'self'-call to a non-satic method in this example working?
class A{
protected function aNonStaticMethod(){
return __class__;
}
public function aEcho(){
echo self::aNonStaticMethod();
}
}
Thanks for explanation.
In your simple example $this and self is interchangable. But be aware of the different method resolving when dealing with inheritance (i added static for completeness):
class A {
protected function aNonStaticMethod(){
return __class__;
}
public function selfEcho(){
echo self::aNonStaticMethod();
}
public function staticEcho(){
echo static::aNonStaticMethod();
}
public function thisEcho(){
echo $this->aNonStaticMethod();
}
}
class B extends A {
protected function aNonStaticMethod(){
return __class__;
}
}
$b = new B();
$b->selfEcho(); // A
$b->staticEcho(); // B
$b->thisEcho(); // B
Calling non-static method statically
Theoretically it should not work, but as this comment says:
There was no static keyword in php4 but php4 did allow for static
calls. To maintain backwards compatibility this was left in when the
static keyword was added in php5.
This comment is supported by this official php.net wiki:
This is already deprecated if the call occurs from an instance method.
Not annotating methods as static is an obsolete PHP4-ism.
You really should not call non-static method statically - it does not make sense (if there is a static keyword).
Avoid calling non-static methods statically completely!
...because a) it is a bad approach and b) the PHP docs say:
Caution
In PHP 5, calling non-static methods statically generates an E_STRICT level warning.
AND
Warning
In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future.
Using :: operator for non-static calls - may be a good approach!
As #Kontrollfreak pointed out and as this docs say the :: operator is not limited to static calls:
the double colon, is a token that allows access to static, constant,
and overridden properties or methods of a class
So it is OK if you reference this way a method or properties from a parent class - which is not limited to a direct parent.
EDIT: do not mistake this for Fascade etc. software patterns!
During writing this answer I forgot to mention that there might be cases, when the call is static, but internally it is calling dynamic method - for more info see patterns like Facade or Singleton.
However do NOT mistake these with issue described above! (issue above is about using direct static call on dynamic thing that should be called dynamically, these patterns are about calling static methods statically, which then may dynamically invoke something dynamic (internally)).
This question already has answers here:
Passing an instance method as argument in PHP
(3 answers)
Closed 9 years ago.
I have a class in PHP like this:
class RandomNumberStorer{
var $integers = [];
public function store_number($int){
array_push($this->integers, $int);
}
public function run(){
generate_number('store_number');
}
}
...elsewhere I have a function that takes a function as a parameter, say:
function generate_number($thingtoDo){
$thingToDo(rand());
}
So I initialise a RandomNumberStorer and run it:
$rns = new RandomNumberStorer();
$rns->run();
And I get an error stating that there has been a 'Call to undefined function store_number'. Now, I understand that that with store_number's being within the RandomNumberStorer class, it is a more a method but is there any way I can pass a class method into the generate_number function?
I have tried moving the store_number function out of the class, but then I then, of course, I get an error relating to the reference to $this out of the context of a class/ instance.
I would like to avoid passing the instance of RandomNumberStorer to the external generate_number function since I use this function elsewhere.
Can this even be done? I was envisaging something like:
generate_number('$this->store_number')
You need to describe the RandomNumberStore::store_number method of the current instance as a callable. The manual page says to do that as follows:
A method of an instantiated object is passed as an array containing an
object at index 0 and the method name at index 1.
So what you would write is:
generate_number([$this, 'store_number']);
As an aside, you could also do the same in another manner which is worse from a technical perspective, but more intuitive:
generate_number(function($int) { $this->store_number($int); });
This question already has answers here:
Error message Strict standards: Non-static method should not be called statically in php
(7 answers)
Closed 9 years ago.
I'm using php, and the following line produces an error I've not been able to fix:
self::$connection = DatabaseConnection::getConnection();
The message is telling you that DatabaseConnection->getConnection(); is not a static method.
The difference is that static methods are called on a class and using the :: operator. Non-static methods (instance methods), are called on an instance of the class and using the -> operator.
PHP allowed non-static methods to be called in a static way, as long as they don't use any instance properties. With error reporting set to strict, it will throw this error though.
To solve it, either create a DatabaseConnection instance to call the method on, or change its declaration to static if it should be a static method.
You can also make error reporting less strict, but that's the wrong way to solve it in my book,
In the following code, nonStatic() is not a static method. Even then I am able to access it without creating an object (in a static way). Could anyone please help me in understanding as this is not possible in other languages like Java?
<?php
class MyClass
{
function nonStatic() {
echo "This can be printed";
}
}
MyClass::nonStatic(); // This can be printed
It's allowed, but it generates an E_STRICT warning:
Error #: 2048, Error: Non-static method MyClass::nonStatic() should not be called statically, assuming $this from incompatible context
In the earlier OO implementations of PHP this was silently allowed, but better practices have since been adopted.
The opposite works without a hitch though:
class Test
{
function foo()
{
echo $this->bar();
}
static function bar()
{
return "Hello world\n";
}
}
$x = new Test;
$x->foo();
This prints Hello world.
It seems as though the developers of PHP didn't see any value in disallowing static access of non-static methods. This is just one of those idiosyncratic features of PHP that doesn't really serve a purpose. It certainly is bad programming practice to call a non-static method statically, but in PHP it is possible. Maybe in a future version of PHP they will disallow this, but for now, it's just part of the language.
Edit:
Thankfully, the opposite is not allowed - you cannot call a static method from an object context. As Jack pointed out below, you can call a static method from an object context - hardly a best practice in the OOP paradigm, but it's allowed.
Not sure, probably some PHP magic (it's a bit like that sometimes), but you shouldn't do it.
Read more here http://php.net/manual/en/language.oop5.static.php
They also show a similar example, but note:
Calling non-static methods statically generates an E_STRICT level warning meaning this magic ability may disappear in future versions. So don't do it :)