Solved

PHP + properties

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How would I construct this INSERT statement in PDO? 6 33
Php logic to add to date card 9 40
Converting PHP triple des decrypt to c# 9 78
xml with php question 5 38
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
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.

696 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