This question already has answers here:
When should I use 'self' over '$this'?
(23 answers)
Closed 7 years ago.
What is the difference between $this and self? When should we use each of them?
$this refers to the current instance. self refers to the current class.
In other words, you can use $this->someMember to refer to an instance member and self::$someStaticMember to refer to a static member.
$this points to current object where as self:: points to current class.
Related
This question already has answers here:
How can I call a static method from a class if all I have is a string of the class name?
(7 answers)
Closed 1 year ago.
I have the string with class name. I want to get a class with this name and call a static methods of this class.
How to make something like this?
$className = "Model";
getClassByName($className)::sayHello();
You can simply call the method on the variable, you might want to wrap an "if" around it to check if the class exists.
$className = "Model";
if (class_exists($className)) {
$className::sayHello();
}
You can check out this 3v4l for a repro case.
This question already has answers here:
How to get the name of child class from base class when an object of child class is created
(4 answers)
Closed 7 years ago.
Is there a magic constant which, when used in a parent class, will return its child's class name, and not the parent?
What you need is a late static binding.
Not sure if you need a constant only. But using get_class($this); in parent you will get the class of the caller(parent or child)
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
$this->var
There's a "->" symbol when we call a variable, and i'd like to know what actually does the "->" means. I tried to do some research but the textbook and the internet don't explain this clealy.
Thanks in advance.
PHP has two Object Operator namely -> and ::
-> is used when you are trying to call a method on an Instance and / or access an Instance property.
:: is used when you want to call a static method or call a parent class's version of a method within a child class.
This question already has answers here:
What's the difference between :: (double colon) and -> (arrow) in PHP?
(6 answers)
Closed 8 years ago.
what is the difference between $object::$variable and $object->variable
both of them can be used to achieve the same but creates difference when the class member-variable is static as follows-
$object::$variable :- This syntax allows the static variable to be achieved through the object
But
$object->variable :- This syntax does not allow the static variable to be achieved through the object.
What is the semantic difference between the two?
$object::$variable and $object->variable
above both are are valid for accessing class property.
Only difference is that $object::$variable is used for access static property where as $object->variable is used for accessing property of class from instance.
For more words refer Amal Murali commented question link.
With $object::$variable you refer to the $variable of the class, $object->variable refers to the $variable of a instance of this class.
This question already has answers here:
Closed 10 years ago.
Possible Duplicates:
What does the PHP syntax $var1->$var2 mean?
Reference - What does this symbol mean in PHP?
I'm looking at a PHP framework's code, and now and then the symbols "->" appear... for example:
$controller->permissionCheck($ret);
What does "->" stand for and what is used for?
In your example, $controller is a PHP object created somewhere, and permissionCheck is a function defined in that object that is being called with the variable $ret being passed to it. Check this: Reference - What does this symbol mean in PHP?.
It's used to address a function or property of a class.
In this case a function of the controller class seems to be called.
Operator -> is for access to the non-static members of $controller object. In your case, the member is the function permissionCheck.