Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access to static class members using $this? [duplicate]

I'm trying to access static class members of a ActiveModel class in another class.

$class = "\ActiveModel\User";
$class::create(); //WORKS!

$this->class = "\ActiveModel\User";
$this->class::create(); //DOESN'T WORK :( - Throws "Incorrect access to static class member" error

What is the correct way to access it with $this->var?

Another example: enter image description here

like image 955
supersan Avatar asked Mar 15 '15 06:03

supersan


People also ask

How do you access members of a static class?

Members of static classes can be accessed directly using the class name followed by a (.) and class member name. Class Members can be methods, fields, properties, or events. A static class can contain only the static members while a non-static class can contain static members.

Can static access the instance members from the same class?

A static method cannot access a class's instance variables and instance methods, because a static method can be called even when no objects of the class have been instantiated. For the same reason, the this reference cannot be used in a static method.

Can static members be accessed outside of the class?

From outside the class, "static variables should be accessed by calling with class name." From the inside, the class qualification is inferred by the compiler.

Can static methods be accessed by other classes?

Static methods can only be called directly (without using the class name) from within the class they are declared, whereas non-static methods can be called from other classes.


2 Answers

The PHP language forbids this kind of construction. There are other examples. (e.g. previously, you cannot direct access an array returned by a function)

$elem = get_array()[0] // Doesn't work until PHP 5.4.0

The main reason behind these limitations is that the parser doesn't like it. Because they are complex, or causes ambiguity, or they want to reserve such constructions for future use.

You can simply workaround this restriction by assigning a temporary variable:

$className = $this->class;
$className::create(); // This will work.
like image 127
Tippisum Avatar answered Sep 21 '22 01:09

Tippisum


You can't do something like $this->class::create(). If you want access to a static member you must use Class::Member. The current implementation of Zend PHP parser supports only static method calls that are made directly on a class name or a variable. Here is the grammar:

%token T_PAAMAYIM_NEKUDOTAYIM ":: (T_PAAMAYIM_NEKUDOTAYIM)"

function_call:
    name argument_list
        { $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }
|   class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
        { $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
|   variable_class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
        { $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
|   callable_expr argument_list
        { $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).

Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.

Static properties cannot be accessed through the object using the arrow operator ->.

Static property example

<?php
class Foo
{
    public static $my_static = 'foo';

    public function staticValue() {
        return self::$my_static;
    }
}

class Bar extends Foo
{
    public function fooStatic() {
        return parent::$my_static;
    }
}


print Foo::$my_static . "\n";

$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n";      // Undefined "Property" my_static 

print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // As of PHP 5.3.0

print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>

Static method example

<?php
class Foo {
    public static function aStaticMethod() {
        // ...
    }
}

Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?>

Read more at:

http://php.net/manual/en/language.oop5.static.php

Call static method with class name stored as instance variable

https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L890

like image 41
Adrian Cid Almaguer Avatar answered Sep 18 '22 01:09

Adrian Cid Almaguer