What does $this refers to when used without -> [duplicate] - php

This question already has answers here:
What does the variable $this mean in PHP?
(11 answers)
Closed 2 years ago.
$this is used internally in a class to point towards internal methods or properties with $this->method();
However in WordPress code, I noticed:
class ABC{
function efg(){
do_action('hookname', $this);
}
}
What is $this on its own supposed to mean?

$this refers to the current instance of the class ABC (an object -- efg() isn't a static method that would be run outside the context of a $this existing).

Related

static keyword before variable in a method [duplicate]

This question already has answers here:
Static variables in PHP
(5 answers)
Closed 11 months ago.
I found the following method and I don't know what the static keyword does at that point. The variable is initialized with [] so it's empty anytime the method is called anyways.
What does the static keyword do at this point?
public function getSomething($entity)
{
static $collection = [];
if (!$collection[$entity->getPrimaryKey()]) {
$collection[$entity->getPrimaryKey()] = 'something';
}
return $this->doCollection($collection);
}
declaring properties or methods as static allows you to access them without needing to instantiate the class.

Using $this when not in object context on yii [duplicate]

This question already has answers here:
PHP Fatal error: Using $this when not in object context
(9 answers)
Closed 4 years ago.
class ModelFile extends CoreModel{
public $var;
public $var2;
public static function getId() {
$this->var = '123';
}
}
I have above model. I am facing issue while using assigned variable.
The error I get on above code is
Fatal error: Using $this when not in object context
Any idea of this issue?
You cannot access $this in static methods because they don't belong to any instance, but to the whole class instead. In your case, just remove static keyword.

instance object can call static method but not static variable [duplicate]

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.

PHP what does :: mean? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've come accross this notation in PHP magento 'Mage::run()'.
What does :: mean? Can't seem to find a simple explanation anywhere.
It allows you to call a static function/method in a class, without making an instance of the class:
class myClass{
static function myFunction(){
echo"foo";
}
}
now call it like this with ::
myClass::myFunction;
with a public function, you would have to make an instance:
$mycalssinstance= new myCalss;
//then call it
$mycalssfunction=$mycalssinstance->myFunction();
It's Scope Resolution Operator (also called Paamayim Nekudotayim). It allows you to access constants or static methods.
Call to a static (class) method. You can call this without an instance of the class.
http://us.php.net/manual/en/language.oop5.paamayim-nekudotayim.php
It is a reference to a static php function
http://php.net/manual/en/language.oop5.static.php

PHP: Difference between -> and :: [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In PHP, whats the difference between :: and ->?
In PHP, what is the main difference of while calling a function() inside a class with arrow -> and Scope Resolution Operator :: ?
For more clearance, the difference between:
$name = $foo->getName();
$name = $foo::getName();
What is the main profit of Scope Resolution Operator :: ?
$name = $foo->getName();
This will invoke a member or static function of the object $foo, while
$name = $foo::getName();
will invoke a static function of the class of $foo. The 'profit', if you wanna call it that, of using :: is being able to access static members of a class without the need for an object instance of such class. That is,
$name = ClassOfFoo::getName();
-> is called to access a method of an instance (or a variable of an instanciated object)
:: is used to access static functions of an uninstanced object
They are for different function types. -> is always used on an object for static and non-static methods (though I don't think it's good practice use -> for static methods). :: is only used for static methods and can be used on objects (as of PHP 5.3) and more importantly classes.
<?php
class aClass {
static function aStaticMethod() {}
function aNormalMethod() {}
}
$obj = new aClass();
$obj->aNormalMethod(); //allowed
$obj->aStaticMethod(); //allowed
$obj::aStaticMethod(); //allowed as of PHP 5.3
$class_name = get_class( $obj );
$class_name::aStaticMethod(); //long hand for $obj::aStaticMethod()
aClass::aStaticMethod(); //allowed
//aClass::aNormalMethod(); //not allowed
//aClass->aStaticMethod(); //not allowed
//aClass->aNormalMethod(); //not allowed

Categories