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.
Related
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.
This question already has answers here:
How to indicate in php 7.1 that the return type is the current child type?
(4 answers)
How to define stricter typing in child class inherited methods in PHP?
(1 answer)
Abstract function parameter type hint overriding in PHP 7
(2 answers)
Closed 12 months ago.
I'm overriding a base class which has a method that takes in an instance of its self, is it possible to make the method take in an instance of the overriding class not the base class? EG:
class Foo {
public function baseMethod() {
}
public static function test(self $foo) {
}
}
class Bar extends Foo {
public function extendingMethod() {
}
public static function test(self $foo) {
$foo->baseMethod();
$foo->extendingMethod();
}
}
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).
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:
Closed 12 years ago.
Possible Duplicate:
PHP: self vs this
Hello,
Could you help me understanding the meaning of the PHP variable name $this?
Thank you for your help.
$this refers to the class you are in.
For example
Class Car {
function test() {
return "Test function called";
}
function another_test() {
echo $this->test(); // This will echo "Test function called";
}
}
Hope this helps.
You might want to have a look at the answers in In PHP5, what is the difference between using self and $this? When is each appropriate?
Basically, $this refers to the current object.
$this is a protected variable that's used within a object, $this allows you to access a class file internally.
Example
Class Xela
{
var age; //Point 1
public function __construct($age)
{
$this->setAge($age); //setAge is called by $this internally so the private method will be run
}
private function setAge($age)
{
$this->age = $age; //$this->age is the variable set at point 1
}
}
Its basically a variable scope issue, $this is only allowed within a object that has been initiated and refers to that object and its parents only, you can run private methods and set private variables where as out side the scope you cannot.
also the self keyword is very similar apart from it refers to static methods within class, static basically means that you cant use $this as its not an object yet, you must use self::setAge(); and if that setAge method is declared static then you cannot call it from an instant of that object / object
Some links for you to get started:
http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/en/language.oop5.static.php
How to explain 'this' keyword in a best and simple way?
When to use self over $this?