PHP __CLASS__ like magic constant respect inheritance? [duplicate] - php

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)

Related

How to get class by name in PHP? [duplicate]

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.

Define properties of a class [duplicate]

This question already has answers here:
How can I access an object property named as a variable in php?
(5 answers)
Closed 2 years ago.
Is there any way to define the properties of a class generic? To be more specific can I have let's say a file with define statement e.g.
define('USER_ID', 'userId')
and a class
foo{$userId}
How can I access the property like
$foo->USER_ID
?
Is there any way to achieve something like the above?
Thanks
You should use class constant :
<?php
class Demo {
const USER_ID = 3;
}
echo Demo::USER_ID;
?>

PHP: What does ::class do? [duplicate]

This question already has answers here:
What does ClassName::class mean in PHP? [duplicate]
(3 answers)
Closed 7 years ago.
I noticed in Laravel this syntax:
Illuminate\Foundation\Providers\ArtisanServiceProvider::class
What does the ::class operator do?
from PHP doc
"Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes."
<?php
namespace NS {
class ClassName {
}
echo ClassName::class;
}
?>
http://php.net/manual/en/language.oop5.basic.php
From the docs:
Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.

what does $this->variable mean [duplicate]

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.

What does :: mean in php? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the “::” notation in php used for?
I noticed this code while modifying a friends code and noticed this piece of code: TestPages::LoadMenu();
what does :: mean in php?
A great answer would mean a lot.
Thanks!
It's the 'Scope Resolution Operator'.
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in
simpler terms, the double colon, is a token that allows access to
static, constant, and overridden properties or methods of a class.
http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php
In layman terms it is used to call static Methods of a Class.
In your example, LoadMenu() is a static function of the TestPages class.
This means that you do not have to create an instance of a TestPages to call LoadMenu()
It is used to access static methods of class, static variables and constants
Read more
It means static class member access, in this case static method invocation.
It's used to access class methods / properties:
http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

Categories