keevitaja Posted October 30, 2009 Share Posted October 30, 2009 hello, i'm having a problem. static::$text variable gets lost at some point. can someone please correct and explain it to me? <?php <?php class A { protected static $text; public static function init($one = '', $two = '') { $obj = new self; $obj->one = $one; $obj->two = $two; $obj->static_text = static::$text; return $obj; } public function output_text() { echo 'here shoult be some static text: ' . static::$text; } } class B extends A { protected static $text = 'this is a static text under php 5.3'; } $b = B::init('first', 'second'); echo "first: {$b->one}<br>second: {$b->two},<br>static text: {$b->static_text}<hr>"; //prints out the static_text $b->output_text(); //doesn't print out the static::text value echo '<hr>'; B::output_text(); //prints out static::$text value echo '<hr>'; $b = new B; $b->output_text(); //prints out static::$text value ?> Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/ Share on other sites More sharing options...
mikesta707 Posted October 30, 2009 Share Posted October 30, 2009 when you use the scope resolution operator you have to use the class name of the scope you are resolving. so echo 'here shoult be some static text: ' . static::$text; should probably be echo 'here shoult be some static text: ' . A::$text; Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947895 Share on other sites More sharing options...
keevitaja Posted October 30, 2009 Author Share Posted October 30, 2009 no. then it never prints it out! it is static binding under PHP 5.3. in PHP 5.2 you will probably get some error! Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947899 Share on other sites More sharing options...
mikesta707 Posted October 30, 2009 Share Posted October 30, 2009 Ahh missed the 5.3 part sorry. Here is the problem, you are using a parent method, and expecting it to use a static variable from a child class. This will not work simply because of scope. The parent has no access to the childs attributes, even its static ones. However, you may want to take this into consideration: As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static). I'm pretty sure you can't qualify the scope with the static key word. In fact, that doesn't even really make sense it all. by the way, when I change your code to A:: i get the following output first: first second: second, static text: here shoult be some static text: here shoult be some static text: here shoult be some static text: Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947911 Share on other sites More sharing options...
Mchl Posted October 30, 2009 Share Posted October 30, 2009 I'm pretty sure you can't qualify the scope with the static key word. In fact, that doesn't even really make sense it all. http://www.php.net/manual/en/language.oop5.late-static-bindings.php Example #4 Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947912 Share on other sites More sharing options...
keevitaja Posted October 30, 2009 Author Share Posted October 30, 2009 A:: will not work.. the actual code i'm trying to create is the model for working with sql database where $text is actually a database name. class A is the parent class and i will create a class for each sql table> B, C and so on! in parent i will do the basic stuff like add, delete etc. it is possible to get the value into this output_text method? Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947915 Share on other sites More sharing options...
mikesta707 Posted October 30, 2009 Share Posted October 30, 2009 I'm pretty sure you can't qualify the scope with the static key word. In fact, that doesn't even really make sense it all. http://www.php.net/manual/en/language.oop5.late-static-bindings.php Example #4 Ahh ok thanks, i didn't see anything on the static keyword page, so I wasn't sure. Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947917 Share on other sites More sharing options...
keevitaja Posted October 30, 2009 Author Share Posted October 30, 2009 once again. my question is PHP 5.3 specific! static binding wasn't present in previous versions! Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947922 Share on other sites More sharing options...
salathe Posted October 30, 2009 Share Posted October 30, 2009 i'm having a problem. static::$text variable gets lost at some point. can someone please correct and explain it to me? In your A::init() method, you create a new instance of "self" (in other words A) and return it. So, $b has an instance of the class A. When $b->ouput_text() is called, static refers to the class to which it belongs (A). When B::output_text() is called, ditto (B). You then make $b an instance of class B, so $b->output_text() uses static pointing to class B. When you say "doesn't print out the static::text value", it actually does; just that the value is NULL (so outputs an empty string). If I can understand your intentions correctly, your A::init() method should be doing $obj = new static; Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947927 Share on other sites More sharing options...
keevitaja Posted October 30, 2009 Author Share Posted October 30, 2009 public static function init($one = '', $two = '') { $obj = new static; thanks you! it made the difference! Quote Link to comment https://forums.phpfreaks.com/topic/179641-solved-lsb-php-53-problem-with-static-value/#findComment-947929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.