2012-05-08 42 views
0

如何獲取frome子類的值作爲主範圍中受保護的ststic屬性。 我在這些行中使用,但它不起作用。如何從主範圍的subclss獲取受保護的stastic值

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

我想查看這些行謝謝。

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

輸出是

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

這裏我的代碼:

<?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(); 

?> 

回答

2

看到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.

可惜你不能 「逼」 子類來定義這個靜態成員一樣你可以用抽象的成員方法。

<?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"; 

打印

B 
C 
Table_shape_B 
Table_shape_C 
+0

靜態使錯誤行「return static :: $ table_name;」 –

+0

(!)致命錯誤:訪問未聲明的靜態屬性:Class_A :: $ table_name –

+0

您使用php 5.3+嗎? – VolkerK

1

[編輯]早餐後,我意識到這個代碼將會是如何的酷是在CMS我工作。並使用數組完成它。

<?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(); 

?> 

舊的輸出,同時完成1:

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

新的輸出:

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

我真的要感謝海報這樣一個有趣的問題。希望這可以幫助。

0

但是爲什麼你需要靜態變量呢?

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"; 
} 
相關問題