Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP + properties

Posted on 2008-06-26
3
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Rob Siklos earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

705 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