benom Posted June 4, 2014 Share Posted June 4, 2014 Hello everyone, I just got a task on my University but I'm not sure if I understend it correctly.I need to find one mistake in this code: class number { public $_a = 3; public function __construct() { $this->_a = 4; } public static function out() { echo self::$_a * 5; } } number::out() I guess the answer will be 15 after I will change public $_a = 3; to static $_a = 3;. It shows number but im not sure if im correct Quote Link to comment https://forums.phpfreaks.com/topic/288984-need-help-with-exercise/ Share on other sites More sharing options...
Jacques1 Posted June 4, 2014 Share Posted June 4, 2014 So what is the mistake in your opinion? Quote Link to comment https://forums.phpfreaks.com/topic/288984-need-help-with-exercise/#findComment-1481878 Share on other sites More sharing options...
benom Posted June 4, 2014 Author Share Posted June 4, 2014 (edited) I was getting an error like this one: Fatal error: Access to undeclared static property: number::$_a So I guess it was problem with making _a visible outside of class. Thats why I changed it. Edited June 4, 2014 by benom Quote Link to comment https://forums.phpfreaks.com/topic/288984-need-help-with-exercise/#findComment-1481879 Share on other sites More sharing options...
Jacques1 Posted June 4, 2014 Share Posted June 4, 2014 (edited) No, this has nothing to do with visibility. Those are all public attributes, and you don't even try to access them outside of the class. The problem is the static modifier. You've found that already, but you somehow didn't draw the right conclusions. A static method belongs to the class itself. A non-static attribute, on the other hand, is associated with a particular instance of the class. That code is trying to mix the two levels: It calls a static method, but then it tries to access a non-static attribute within the method. This is invalid and simply makes no sense. The class simply doesn't have a $_value attribute (but its instances have). Edited June 4, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/288984-need-help-with-exercise/#findComment-1481882 Share on other sites More sharing options...
benom Posted June 4, 2014 Author Share Posted June 4, 2014 Ok, Thank you for a fast answers. Quote Link to comment https://forums.phpfreaks.com/topic/288984-need-help-with-exercise/#findComment-1481886 Share on other sites More sharing options...
gizmola Posted June 4, 2014 Share Posted June 4, 2014 To boil this down for you, you need to understand the difference between self and this, and when to use those, as well as how static methods work, and when those are appropriate (or not) to use in a class. The original code has a couple of different implications. For example the constructor is setting the initial value of the $_a class variable. Is that important for the class to retain? If so, it doesn't make sense to me that you would have a static class variable, which it doesn't at present. Quote Link to comment https://forums.phpfreaks.com/topic/288984-need-help-with-exercise/#findComment-1481893 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.