문제

I'm learning Prado php framework for a while now, and I've been wondering about one feature, if it's from PHP or somehow implemented in Prado.

Namely, classes used in Prado can utilize properties (fields) that aren't declared in the class itself, but 'defined' by set and get methods.

Here's an example:

class myClass extends somePradoClass {

    public function myPradoMethod() {
        $MyVariable = 22;
        echo $MyOtherVariable; // this one is read only (only get method defined)
    }

    public function getMyVariable() {
        return 0;
    } 

    public function setMyVariable($value) {
        $this->isFieldFromParentClass = $value;
    }

    public function getMyOtherVariable() {
        return $this->isOtherFieldFromParentClass;
    }

}

Now, somehow it is perfectly fine to use $MyVariable and $MyOtherVariable throughout the class, as if they were declared as class properties.

So, question again: is this a PHP or Prado feature?

Thx

도움이 되었습니까?

해결책

This is neither a PHP feature nor a Prado feature. I don't know Prado, but PHP has not such feature, so Prado can't have it either ;)

What you are looking for however, are things such as this: $this->myUndefinedMember = $something

Your example uses local variables, these cannot be set and read from automagically.

This will invoke the magic __set method, if defined that is.

Prado could (I don't know if it does) define this method for a certain superclass that you usually use and then dynamically check whether a setter method has been defined for that variable name.

The signature is as follows:

public function __set($name, $value)

The maigc method __get($name) works analogously.

If you do not set it as public, you will only be able to use this property-like feature from within the class( or subclasses).

As a reference, see here on PHP5's feature or overloading properties and methods.

Update

A sample implementation could look like this:

class MyMagicSuperClass{
    public function __get($name){
        $getter_name = 'get'.ucwords($name);
        if(method_exists($this, $getter_name){
            return $this->$getter_name();
        }

        //error handling
    }

    public function __set($name, $value){
        $setter_name = 'get'.ucwords($name);
        if(method_exists($this, $setter_name){
            return $this->$setter_name($value);
        }

        //error handling
    }
}

다른 팁

I answer very late to this but I felt the answers posed very confusing to a simple feature.

To give a much simpler answer. Any Prado classes that inherits from TComponent use the __set and __get functions.

More about php magic functions here

So if u have a function that starts with "get" or "set" they will be called when use the rest of the function name as fields.

This is the actual prado code for __get the __set is about the same

    public function __get($name)
{
    $getter='get'.$name; $jsgetter = 'getjs'.$name;
    if(method_exists($this,$getter))
    {
        // getting a property
        return $this->$getter();
    }
    else if(method_exists($this,$jsgetter))
    {
        // getting a property
        return (string)$this->$jsgetter();
    }
    else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
    {
        // getting an event (handler list)
        $name=strtolower($name);
        if(!isset($this->_e[$name]))
            $this->_e[$name]=new TList;
        return $this->_e[$name];
    }
    else
    {
        throw new TInvalidOperationException('component_property_undefined',get_class($this),$name);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top