2012-05-08 38 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"; 
} 
相关问题