Ask a Question
Advertise on boostr.in
Organizational Research By
Surprising Reserch Topic
Experts Most Trusted Topic
php class def individual accessors mutators or set with switch using -'php,oop,coding-style'
php class def individual accessors mutators or set with switch ¬†using -'php,oop,coding-style'
When defining a PHP class, which is preferred/best practice? Are there any key differences I'm overlooking?
It seems like it could be more clean, concise, and convenient to write a __set() magic method and put a switch() construct in it with cases for all the private members to which I want to allow access. It wouldn't be called automagically from inside the class, but then again neither would setFoo(), so if I want to use the accessor/mutator internally, I'd have to explicitly call a method either way.
The other difference is that in the code outside the class, I could always access member vars in the same fashion as $obj->foo, whether public (directly) or private (using __set()), versus using many separate methods.
I guess this comes down mostly to an aesthetic choice. For example, if I have address data on a purchase, I don't want to have 16 or more separate accessor methods just for first name, last name, address1, address2, city, state, etc. each for shipping and billing data.
Are there any key differences I've overlooked? (Might a sophisticated IDE refuse to auto-complete a member name outside the class because it's marked as private?) Have I pretty much answered my own original question? Thanks in advance for your input.
Sep 7, 2015
to add a comment.
Related Hot Questions
Walkin Jobs Opening
Government Jobs Opening