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();
?>
see http://docs.php.net/language.oop5.late-static-bindings
Unfortunately you can't "force" a subclass to define this static member like you can with abstract member methods.
prints
But why do you need static variables for that?
[EDIT] After breakfast, I realised how cool this code would be in a CMS I'm working on. And completed it utilizing arrays.
The old output, done 1 at a time:
The new output:
I really have to thank the poster for such an intriguing question. Hope this helps.