Solved

PHP + properties

Posted on 2008-06-26
3
189 Views
Last Modified: 2008-06-26
Hi,

If I understand properties (PHP 5+) correctly, your class has one __set() and one __get() method, like:

function __get($propName) {
    if ($propName == 'size') {
       ...
    }
    else if ($propName == 'height') {
        ...
    }
}

function __set($propName, $value) {
    if ($propName == 'size') {
       ...
    }
    else if ($propName == 'height') {
        ...
    }
}


How does that scale once you start getting a lot of properties in your class? It seems similar to doing a linear search in an array.

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Rob Siklos earned 500 total points
ID: 21876946
Your understanding is correct.  Often __get and __set are used so that you don't have to actually hardcode the names of your properties.  For instance, if you are using a simple property bag.  For example:
class MyClass {
 
    private $propertyBag = array();
 
    function __get($propName) {
       return $propertyBag[$propName];
    }
 
    function __set($propName, $value) {
        $propertyBag[$propName] = $value;
    }
}

Open in new window

0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21876957
The above is obviously a trivial example, but it's great because you can handle new properties without modifying any code, and you can change the implementation or add error checking without changing the API you expose.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21881010
I see, thanks.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question