overloading - How do I use PHP's __get() and __set() for defined fields in my class? -


I have read, and most examples show only to allow custom fields to define (similar to StdClass).

But what about my private areas defined in my class? How should this be recovered / assigned? I can switch one to the possible values ​​and work on some values:

  class A {Private $ name; Private $ age; Public function __get ($ var) {switch ($ var) {case 'name': return $ this-> Name; break; Case 'age': return $ $ this- & gt; Age + 10; // just to separate something;) Break; Default: Break; }}}  

Is this the best way, or is there any other generally best practice? (I do not know that it is possible for the loop class variables inside the classroom, but is not suitable in most situations, because you do not want to return everything.)

This will effectively make them public, and generally it does not get anything from you instead of using public properties (but you do not get the performance penalty Pay and you Programming have to write more code, risk bugs).

Use it only if you have an existing code that uses public property and you need a gator / setter (like age ) with your Examples).

If you need to execute some code, when the property is read / written (for example, to get it read-only or familiar from the database), you will be given the usual recipients / Sets ( getAge () / setage () ).


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -