1

I'm trying to come up with a graceful way to create C#-esque properties in PHP

Right now, I have:

class Example
{
    private $allowedProps = array('prop1', 'prop2', 'prop3');
    private $data = array();

    public function __set($propName, $propValue)
    {
        if (in_array($propName, $this->allowedProps))
        {
            // set property
        }
        else
        {
            // error
        }
    }

    public function __get($propName)
    {
        if (array_key_exists($propName, $this->data))
        {
            // get property
        }
        else
        {
            // error
        }
    }
}

In the commented out sections, for something more complex than simply writing to or retrieving from the $data array, the easiest thing to do would be to simply branch logic where necessary with an if-else or switch. That's messy, though, and runs counter to what I want. Is there a way to invoke a callback function that would have access to the $data array?


EDIT: Seems like the simplest thing to do would be:

class Example
{
    private $allowedProps = array('prop1', 'prop2', 'prop3');
    private $data = array();

    public function __set($propName, $propValue)
    {
        $propName = strtolower($propName);

        if (in_array($propName, $this->allowedProps))
        {
            $funcName = "set" . ucfirst($propName);
            $this->$funcName($propValue);
        }
        else
        {
            // error
        }
    }

    public function __get($propName)
    {
        $propName = strtolower($propName);

        if (array_key_exists($propName, $this->data))
        {
            $funcName = "get" . ucfirst($propName);
            $this->$funcName();
        }
        else
        {
            // error
        }
    }

    private function getProp1()
    {
        // do stuff, and return the value of prop1, if it exists
    }

    // ...
}

I'm not sure if having a host of private setter and getter methods is the way to go. Lambdas would probably be ideal, but they're only available in PHP 5.3+.

Major Productions
  • 5,914
  • 13
  • 70
  • 149
  • 1
    Could you use something along the lines of the example [here](http://forums.whirlpool.net.au/archive/953482)? Also might be interested in this question: [How do I implement a callback in PHP?](http://stackoverflow.com/questions/48947/how-do-i-implement-a-callback-in-php) – Roman Jul 01 '11 at 16:25
  • By the way, the C# way of creating a property is to have a get_Foobar() method and a set_Foobar(val) method. In fact, if you create these methods in a class and compile them, they will appear as a single "Foobar" property with a getter and setter. – Jon Davis Jul 01 '11 at 16:47
  • True, but I'm trying to see if I can do something complex, per individual class data member, without resorting to explicit setter/getter methods. Having something like $stock->UpdatedQuote rather than $stock->GetUpdatedQuote() – Major Productions Jul 01 '11 at 16:51

1 Answers1

0

Not really an answer, per se, but I figured I'd mark it as solved for now as I haven't had the time to experiment further. Of course, any additional suggestions are welcome.

Major Productions
  • 5,914
  • 13
  • 70
  • 149