PHP static properties and const overriding [duplicate] - php

This question already has answers here:
What is the difference between self::$bar and static::$bar in PHP?
(5 answers)
Closed 3 years ago.
I'm wanting to create an class inheriting (extending) another PHP class with a protected const that I want to override in my extending class.
I created a parent class (A for the example) and an inheriting class (B for the example). class A has a protected const (named CST) defined. class B overrides this const as well.
When calling a method class B inherited from A that displays self::CST, the printed value is the CST value from A, not the const CST overrided in B.
I observe the same behavior with a static property named $var.
It seems that self used in methods is always refering to the defining class (A in my example) and not the class used for calling the static method.
class A
{
protected static $var = 1;
protected const CST = 1;
public static function printVar()
{
print self::$var . "\n";
}
public static function printCST()
{
print self::CST . "\n";
}
}
class B extends A
{
protected static $var = 2;
protected const CST =2;
}
A::printVar();
A::printCST();
B::printVar();
B::printCST();
Is there a way to permit my static method printCST() to display 2 when called with B::printCST() without rewriting the method in class B and to benefit code reusability of OOP?

Dharman suggested to use static::CST instead of self::CST.
This was the solution to my problem.

Related

How to access static property of child class in php? [duplicate]

This question already has answers here:
Overriding class constants vs properties
(2 answers)
Inheritance of static members in PHP
(3 answers)
Closed 4 years ago.
<?php
class A{
static $var;
function test(){
var_dump(self::$var);
}
}
class B extends A{
static $var = 'something';
}
$b = new B();
$b->test();
?>
why does this print out null and how do I fix this? It works if I do not set $var as static but i need it to be accesible without creating an instance.
$b->test(); prints null because it is null. When you do this:
class B extends A{
static $var = 'something';
}
you're not actually doing anything to the $var property in your A class. The property is defined with the static keyword, and per the docs, "A property declared as static cannot be accessed with an instantiated class object (though a static method can)." Thus you cannot inherit it (or subsequently set it) from your B class.
If you want the test() method to output anything meaningful, you need to set it statically, such as:
A::$var = "something";

PHP get_called_class() as variable for referencing static property

I'm trying every variation of the following to refer to a static property:
get_called_class()::$$prop
I've tried this:
${get_called_class()}::$$prop
I've tried many things, but can't seem to get it.
I know I can just do this:
$className = get_called_class();
$className::$$prop
BUT, that means an extra line of code. Surely there must be a way for the language to make this work on the same line. Anyone have a solution?
(By the way, the static property is protected, so it fails with ReflectionClass::getStaticPropertyValue.)
Without understanding any additional context here, you don't need to actually invoke get_called_class to poke at LSB-resolved static properties. Instead, use the static keyword to automagically resolve the currently called static class name.
class A {
static $foo = 'from a';
public static function test($property) {
echo static::$$property, "\n";
}
}
class B extends A { static $foo = 'from b'; }
class C extends A { static $foo = 'from c'; }
Example from the PHP interactive prompt:
php > include '/tmp/get_called_class.php';
php > A::test('foo');
from a
php > B::test('foo');
from b
php > C::test('foo');
from c
php >

using self instead of this variable [duplicate]

This question already has answers here:
When should I use 'self' over '$this'?
(23 answers)
Closed 9 years ago.
I'm reading a book of php and found this code:
class Employee {
static public $NextID = 1;
public $ID;
public function _ _construct( ) {
$this->ID = self::$NextID++;
}
public function NextID( ) {
return self::$NextID;
}
}
why here is used self::$NextID++; can I use like this:
$this-ID = $this->$NextID++;
Because in php you have to reference static functions with self.
There was also an explanation on stackoverflow already: see here
When a class is called statically ie. ClassName::someMethod(), there is no "instance" of the class.
Since $this refers to the instance of the class, $this will not exist when your class is used statically. (so $this will only be available when you created an object of your class by using $var = new ClassName())
self refers to the class (not the object) so in static classes you can use self::.. to refer to properties or methods within the class.

the correct way to access a static arrays from a static function inside a PHP class [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how do i access static member of a class?
I have this class. In its createNew function I refer to $reftbArr = tc_group::$tblFields;.
I have many classes similar to this class a couple of times. They have the same methods and variables but of course the class name is different.
How is the best way in the function createNew to access tc_group::$tblFields; but not have the hardcoded class name?
<?php
class tc_group {
public $id;
public $password;
private static $tableName = "tc_group";
public static $tblFields = array(
':id' => array('value' => '','required' => 0),
':password' => array('value' => '','required' => 0)
);
public static function createNew($link , $tblfields){
$reftbArr = tc_group::$tblFields;
}
}
?>
You can use self for example: self::$variable
You can use static for example: static::$variable
The difference is that self will access the current class it is used in (ignoring inheritance), static will obey inheritance.
Extended Answer
I have this same class a couple of times. they have the same methods and vars but of course the class name is different.
That sounds like it needs refactoring. In which case create an "abstract class" which defines all the properties and methods defined by the other classes and remove them from the other classes (as they are now in the abstract class). Then extend the abstract class to create the separate classes as needed that contain ONLY WHAT CHANGES in them.
$tableFields = self::$tblFields;
print_r($tableFields);
You would use the static keyword self.
public static function createNew($link , $tblfields){
$reftbArr = self::$tblFields;
}

In PHP, are there any advantages to using forward_static_call_array() instead of call_user_func_array() when dynamically calling a static method?

Specifically, is one more efficient than the other?
There is at leat two differences between forward_static_call_array and call_user_func_array :
The first one only exists since PHP 5.3
The first one must be called from inside a class
After that, I suppose there is some difference that's related to Late Static Binding, that was introduced with PHP 5.3.
Actually, if you take a closer look at the given example, it seems to be exactly that : the "context" of the class inside which you are using forward_static_call_array is "kept", in the called method.
Considering this portion of code, that's derived from the given example :
class A {
const NAME = 'A';
public static function test() {
$args = func_get_args();
echo static::NAME, " ".join(',', $args)." \n"; // Will echo B
}
}
class B extends A {
const NAME = 'B';
public static function test() {
echo self::NAME, "\n"; // B
forward_static_call_array(array('A', 'test'), array('more', 'args'));
}
}
B::test('foo');
You'll get this output :
B
B more,args
i.e. from the method in class A, you "know", via the static:: keyword, that you're "coming from B".
Now, if you try to do the the same thing with call_user_func :
class B extends A {
const NAME = 'B';
public static function test() {
echo self::NAME, "\n"; // B
call_user_func_array(array('A', 'test'), array('more', 'args'));
}
}
(the rest of the code doesn't change)
You'll get this output :
B
A more,args
Note the A on the second line ! With forward_static_call_array, you didn't get an A, but a B.
That's the difference : forward_static_call_array forwards the static context to the method that's called, while call_user_func_array doesn't.
About your efficiency question : I have no idea -- you'd have to benchmark ; but that's really not the point : the point is that those two functions don't do the same thing.

Categories