This question already has answers here:
PHPUnit - Use $this or self for static methods?
(3 answers)
When should I use 'self' over '$this'?
(23 answers)
Closed 3 years ago.
I was messing around and discovered that you could actually call static methods with $this->method()
And it got me a bit confused and curious about the differences between the 3 ways (that I know of) to call static methods
$this->method();
static::method();
self::method();
Now, I think I understand the difference between the latter two, but what about the first one?
It is important to understand the behavior of static properties in the context of class inheritance:
Static properties defined in both parent and child classes will hold DISTINCT values for each class. Proper use of self:: vs. static:: are crucial inside of child methods to reference the intended static property.
Static properties defined ONLY in the parent class will share a COMMON value.
declare(strict_types=1);
class staticparent {
static $parent_only;
static $both_distinct;
function __construct() {
static::$parent_only = 'fromparent';
static::$both_distinct = 'fromparent';
}
}
class staticchild extends staticparent {
static $child_only;
static $both_distinct;
function __construct() {
static::$parent_only = 'fromchild';
static::$both_distinct = 'fromchild';
static::$child_only = 'fromchild';
}
}
$a = new staticparent;
$a = new staticchild;
More on https://www.php.net/manual/en/language.oop5.static.php
self using for static method and variable in class
$this using for non static method and variable
static generally using call child class of static methods or variable
For example late static
For example late static binding
Related
This question already has answers here:
Is it possible to declare a method static and nonstatic in PHP?
(6 answers)
Closed 3 years ago.
I have a class "A" containing a static method. Now I want a copy of that class(whether by extension or not) except I need that static method to become non-static in the new class.
Any ideas are welcome. Thanks in advance.
I tried extending but when redeclaring the method as non-static I got an error:
class A {
protected $key = null;
static function methodX($args) {
// ...
}
}
class B extends A {
protected $key = "key";
public function methodX($args) {
// ...
return $this->key;
}
}
The actual flavor of overriding is linked with an object. Overriding is when we pass a child class object to a parent class reference variable but the static methods are associated with class they are not linked with an object so we can’t override a static method
The answer here is no. What is the reason? Well, because non-static member variables of a class always belong to an object – meaning that every object has it’s own personal copy of non-static member variables (also known as instance variables). And, static functions have no object to work with since they belong to the class as a whole. Remember that you can call a static function without an object – and for that exact reason a static function can not call instance variables.
you can override only static method to static method.
But you can't, you have to override it as static or create a new function for non-static in your child.
This question already has answers here:
PHP: What if I call a static method in non-static way
(3 answers)
Closed 6 years ago.
I am trying to wrap my head around the static keyword in PHP. Here I wrote a small piece of code where my person1 instance of Human can call a static method called sayRealname(). But when I try to call a public static variable it gives an error. What is the reason for that?
class Human{
public static $age=34;
public static $name='humpty dumpty';
protected static $realname='al';
public static function sayRealname(){
echo self::$age;
}
}
$person1=new Human();
echo $person1->name; // error
$person1->sayRealname(); // prints 34
When you're accessing static content, you need to use the static operator ::.
On the other hand, the arrow operator -> is meant to be used in an instance context, which belongs to the particular instance upon it's being called.
You can read more about this in this part of the documentation.
This question already has answers here:
PHP: differences in calling a method from a child class through parent::method() vs $this->method()
(2 answers)
Closed 9 years ago.
How is it possible to call a overwritten parent function recursively from a child instance? Since I'm not using static methods I can't use parent::method() but have to do somehow different. I have a feeling that there is a better approach to my problem, maybe through a design pattern. This is my class structure:
class Creature {
public function eat() {
// feed and digest
}
}
class Mammal extends Creature {
public function eat() {
// gather and chew
}
}
class Human extends Mammal {
public function eat() {
// have a meal
}
}
class WesternPerson extends Human {
public function eat() {
// use knife & fork
}
}
$me = new WesternPerson();
$me->eat();
Calling eat() shall go recursively upwards from the special to the very abstract class. Most simple way would be if I could call the parent function with the same name from within the overwritten one. But I know, it's overwritten, so I can't.
The :: isn't just for static methods. It is the scope resolution operator. It is used any time you want to utilized a function that exists in scope other then your contextual scope.
parent, static, and self are language constructs that reference different scopes that relate to your calling scope.
So in this case
parent::eat()
will call the Human::eat in the WesternPerson::eat context (ie $this will mean the WesternPerson Instance in the Human::eat context)
As stated in comments, calling parent::eat() would work just fine
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: self vs. $this
I've found that I can call class methods by $this:: prefix. example:
class class1 {
public function foo()
{
echo "1";
}
public function bar()
{
$this::foo();
//in this example it acts like $this->foo() and displays "2"
//using self::foo() displays "1"
}
}
class class2 {
public function foo()
{
echo "2";
}
public function bar()
{
class1::bar();
}
}
$obj = new class2();
$obj->bar(); // displays "2"
class1::bar(); // Fatal error
I want to know whats the diffrence of calling method with $this-> and $this:: prefixes.
ps:
There is a page about diffrence of $this->foo() and self::foo() in this link:
When to use self over $this?
The answer that you linked tells you exactly what you're looking for ;).
Basically, there are two concepts that some people have a tough time differentiating in programming languages: objects and classes.
A class is abstract. It defines a structure of an object. The properties and methods that an object would contain, if an object were built from that class. Creating an object is what you do when you call new class1(). This is instructing PHP to create a new object with all of the properties and methods on the class1 class.
The important thing to note about creating an object is that it also has its own scope. This is really where $this vs static:: (note: do not use self:: or $this::, please use static:: (more on this later)) come in to play. Using $this is instructing PHP to access the properties and methods of your current object. Using static:: is instructing PHP to access the properties and methods of the base class that your object is constructed from.
Here's an example:
class MyClass {
public $greeting;
public static $name;
public greet() {
print $this->greeting . " " . static::$name;
}
}
$instance1 = new MyClass();
$instance1->greeting = 'hello';
$instance2 = new MyClass();
$instance2->greeting = 'hi';
MyClass::$name = 'Mahoor13';
$instance1->greet();
$instance2->greet();
I didn't test the above, but you should get:
hello Mahoor13
hi Mahoor13
That should give to a general idea of the difference between setting a class property and setting an instance property. Let me know if you need additional help.
Edit
$this:: appears to just be a side effect of the way that PHP handles scopes. I would not consider it valid, and I wouldn't use it. It doesn't appear to be supported in any way.
the $this-> is called from instance , so you have to have an instance of the object to use this call.
but $this:: is a static method inside the class you can call it without instance so you do not need to alloc an instance of the class to call it like
myclass::doFoo();
public static function doFoo(){ .... }
and the do function should be static function to call it like this or the php will give error in the newer versions
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: Static and non Static functions and Objects
Why would I use the static key word in php? Is it only to create "good" code or does it have some advantages as well?
function useThisAsStatic(){}
static function useThisAsStatic(){}
public static function useThisAsStatic(){}
public function useThisAsStatic(){}
To clarify the question; all above methods can be used by calling
Object::useThisAsStatic();
And thus suggests that there is no difference in declaring a method to be static.
Is it only to create "good" code
Actually it's to create bad code.
Read why
And this article is more specified about static (thanks to Gordon)
Static functions mean you can call them without instantiating the class (creating the object) first.
Useful in certain situations, but static functions cannot make use of the local members of the class outside of that function.
static means belonging to the Class. You can call a static method or field. Without creating instance of the class.
class NewClass {
public $public_var = "I'm public property!";
public static $static_var = "I'm static property!";
public function public_method()
{
return "I'm public method!";
}
public static function static_method()
{
return "I'm static method!";
}
}
Now you can call NewClass::$static_var and NewClass::static_method() without creating NewClass instance.
While to call public_var and public_method you should create new instance of class:
$c = new NewClass();
echo $c->public_var;
echo $c->public_method();