Jaswinder Posted June 18, 2013 Share Posted June 18, 2013 i am learning static keyword.. and find this example , but it shows error ,, which i highlited below <?phpclass a{static protected $test="class a";public function static_test(){echo static::$test; // Results class b here the program shows error .... can we use static keyword to call the variable or we have to use a class name?echo self::$test; // Results class a}}class b extends a{static protected $test="class b";}$obj = new b();$obj->static_test();?> getting confused about these two terms.. i tell you what i understand till now about them... make me clear plz Scope resolution (::) - this is used to call constants and static variables, static functions and classes but not with instance/object ?? am i right ?? anything more about it ? static keyword - we can call static variable and functions without the need of instantiation i.e directly using scope resolution.... the class also becomes static , if any property or method declared static in it. After inheriting from such class if we override property or method we have to use static keyword again while override,,,, ok or i need to learn more ?? Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted June 18, 2013 Solution Share Posted June 18, 2013 (edited) static::$test only works if you have PHP 5.3+. If you don't then it will definitely create an error. Scope resolution ( - this is used to call constants and static variables, static functions and classes but not with instance/object ??1. Static things (constants, static variables, static methods) 2. Used with self, parent, and static keywords and even for instance methods (eg, parent::__construct) static keyword - we can call static variable and functions without the need of instantiation i.e directly using scope resolution....Yes: static variables, static methods, and with PHP 5.3+ the statically-called class. the class also becomes static , if any property or method declared static in itNo. PHP does not have static classes. You could call a class "static" if you so wanted by marking it final and making the constructor private but it's not a true static class. After inheriting from such classIt's not true inheritance: self:: and parent:: don't respect the inheritance hierarchy (static:: does) and you don't have to respect the parent method's signature. if we override property or method we have to use static keyword again while override,,,,Yes because they're static. Can't create a new non-static property/method with the same name. Edited June 18, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted June 18, 2013 Author Share Posted June 18, 2013 i am using php 5.4.7 ... but still not working static::$test :/ Quote Link to comment Share on other sites More sharing options...
requinix Posted June 18, 2013 Share Posted June 18, 2013 Works for me. What error are you getting? Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted June 19, 2013 Author Share Posted June 19, 2013 error is that when i write that code.. dreamweaver shows than line in red color... means error.. so programs doesn't running at all Quote Link to comment Share on other sites More sharing options...
kicken Posted June 19, 2013 Share Posted June 19, 2013 (edited) Just because Dreamweaver says it is an error, doesn't mean it is. Since the syntax is relatively new, your version of Dreamweaver may just not recognize it. You need to actually run the PHP file and see if you get an error from PHP. Edited June 19, 2013 by kicken Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted June 20, 2013 Author Share Posted June 20, 2013 k,, now problem solved.. thank you Quote Link to comment 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.