SchweppesAle Posted December 31, 2009 Share Posted December 31, 2009 Hey, I was hoping someone could explain the purpose behind var data members in php OOP. Example: class yadayada{ var $_dosomething; public function dosomething() { $result = 'jump'; return $this->_dosomething = $result; } } why not just return $result? Quote Link to comment https://forums.phpfreaks.com/topic/186798-data-members/ Share on other sites More sharing options...
SchweppesAle Posted December 31, 2009 Author Share Posted December 31, 2009 Nevermind, I guess if you set a data member via the constructor it makes sense since all the functions can then call on that variable. I was just wondering why I see regular functions setting them all the time. Like maybe there's a scenario where this would come in handy that I'm not taking into account. Quote Link to comment https://forums.phpfreaks.com/topic/186798-data-members/#findComment-986434 Share on other sites More sharing options...
premiso Posted December 31, 2009 Share Posted December 31, 2009 I am not sure what context you are using "regular functions" in, but in OOP, why they would set a data member in them could be to call back on that variable, give it access to other functions or do a "last" run on that variable. It all depends on how it is coded and it's purpose. Quote Link to comment https://forums.phpfreaks.com/topic/186798-data-members/#findComment-986458 Share on other sites More sharing options...
.josh Posted December 31, 2009 Share Posted December 31, 2009 it's a matter of encapsulation, or scope. In general, you want to have a getter and setter method for your properties, and have other methods (or other things outside of the class) access and change the value of your properties through its getter and setter methods. This prevents direct access to the property. For example, let's say you have the property $weight, and you expect it to at all times be an integer. Well directly accessing it does not guarantee that it will always be type int. Anything can randomly assign some other data type to it, which could cause other methods, functions, etc... to fail, since they may depend on it being type int. So you make $weight private or protected, and you have a setter method for it, and in your setter method, you make sure that the value is cast as int, or within a certain range, format, etc.. (whatever is relevant). It's kind of same principle as form validation, except for class properties. So instead of doing $weight = 123; you would do $this->set_weight('123'); Quote Link to comment https://forums.phpfreaks.com/topic/186798-data-members/#findComment-986484 Share on other sites More sharing options...
SchweppesAle Posted December 31, 2009 Author Share Posted December 31, 2009 it's a matter of encapsulation, or scope. In general, you want to have a getter and setter method for your properties, and have other methods (or other things outside of the class) access and change the value of your properties through its getter and setter methods. This prevents direct access to the property. For example, let's say you have the property $weight, and you expect it to at all times be an integer. Well directly accessing it does not guarantee that it will always be type int. Anything can randomly assign some other data type to it, which could cause other methods, functions, etc... to fail, since they may depend on it being type int. So you make $weight private or protected, and you have a setter method for it, and in your setter method, you make sure that the value is cast as int, or within a certain range, format, etc.. (whatever is relevant). It's kind of same principle as form validation, except for class properties. So instead of doing $weight = 123; you would do $this->set_weight('123'); ok, so generally speaking. I would allow other classes access to the data member and all other functions which exist within that same class would enforce specific data types or properties within the same data type. Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/186798-data-members/#findComment-986485 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.