How to get the protected stastic value from subcls

2019-09-13 00:04发布

问题:

How can i get the value frome child class as a protected ststic attribute in the main scope. I use in these lines but it doesn't work.

self::$table_name="Table_shape_B";
self::$table_name="Table_shape_C";

I want to see these lines Thanks.

selected Database Table name: Table_shape_B
selected Database Table name: Table_shape_C

the output are

new B : are created :
new C : are created :
selected Database Table name:
selected Database Table name:

Here my code:

<?php
    abstract class Class_A {

      protected static $table_name;
         //Class_B Database Table name =  "Table_shape_B"
         //Class_CA Database Table name =  "Table_shape_C"
        public function __construct()   {
            echo "<br />"." new ".get_class($this)." : are created :";
        }

        public function get_table_name_protected() {
            return self::$table_name;
        }
    }

    class B extends Class_A {
        //self::$table_name="Table_shape_B";
    }

    class C extends Class_A     {
     //self::$table_name="Table_shape_C";
    }

    $NewObject1= new B ( );
    $NewObject2= new C ( );

    echo "<br />".' selected Database Table name: '.$NewObject1->get_table_name_protected();
    echo "<br />".' selected Database Table name: '.$NewObject2->get_table_name_protected();

 ?>

回答1:

see http://docs.php.net/language.oop5.late-static-bindings

As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. [...] "Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. It was also called a "static binding" as it can be used for (but is not limited to) static method calls.

Unfortunately you can't "force" a subclass to define this static member like you can with abstract member methods.

<?php
abstract class Class_A {

    public function __construct()   {
        echo get_class($this), "\n";
    }

    public function get_table_name_protected() {
        return static::$table_name;
    }
}

class B extends Class_A {
    protected static $table_name="Table_shape_B";
}

class C extends Class_A     {
    protected static $table_name="Table_shape_C";
}

$NewObject1= new B ( );
$NewObject2= new C ( );

echo $NewObject1->get_table_name_protected(), "\n";
echo $NewObject2->get_table_name_protected(), "\n";

prints

B
C
Table_shape_B
Table_shape_C


回答2:

[EDIT] After breakfast, I realised how cool this code would be in a CMS I'm working on. And completed it utilizing arrays.

<?php

error_reporting(E_ALL);

abstract class Class_A {

    protected static $table_name = array();

    public function __construct()   {
        $tmp = get_class($this);
        echo "<br />"." new ".$tmp." : are created :";
        self::$table_name[$tmp] = "Table_shape_" . $tmp;
    }

    public function get_table_name_protected() {
        $tmp = get_class($this);
        return self::$table_name[$tmp];
    }
}

class B extends Class_A {

}

class C extends Class_A {

}

$NewObject1= new B( );

$NewObject2= new C( );

echo "<br />".' selected Database Table name: '.$NewObject1->get_table_name_protected();

echo "<br />".' selected Database Table name: '.$NewObject2->get_table_name_protected();

?>

The old output, done 1 at a time:

 new B : are created :
 selected Database Table name: Table_shape_B
 new C : are created :
 selected Database Table name: Table_shape_C

The new output:

 new B : are created :
 new C : are created :
 selected Database Table name: Table_shape_B
 selected Database Table name: Table_shape_C

I really have to thank the poster for such an intriguing question. Hope this helps.



回答3:

But why do you need static variables for that?

abstract class Class_A
{

    /**
    * Hold table name
    * @var string
    */
    protected $table_name = null;

    /**
    * Get protected table name
    * @return string
    * @throws RuntimeException
    */
    public function get_table_name_protected()
    {
        if (null === $this->table_name) {
            throw new RuntimeException('Table name is not defined');
        }
        return $this->table_name;
    }
}

class Class_B extends Class_A
{
    protected $table_name = "Table_shape_B";
}

class Class_C extends Class_B
{
    protected $table_name = "Table_shape_C";
}


标签: php oop static