Solved

PHP + properties

Posted on 2008-06-26
3
184 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now