Jump to content

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/186798-data-members/#findComment-986434
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/186798-data-members/#findComment-986458
Share on other sites

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');

Link to comment
https://forums.phpfreaks.com/topic/186798-data-members/#findComment-986484
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/186798-data-members/#findComment-986485
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.