views:

90

answers:

2

Ok i have a problem, sorry if i cant explaint it clear but the code speaks for its self.

i have a class which generates objects from a given class name; Say we say the class is Modules:

public function name($name)
{

   $this->includeModule($name);

   try
   {
       $module    = new ReflectionClass($name);
     $instance = $module->isInstantiable() ? $module->newInstance() : "Err";

      $this->addDelegate($instance);
   }

   catch(Exception $e)
   {
      Modules::Name("Logger")->log($e->getMessage());
   }
   return $this;
 }

The AddDelegate Method:

protected function addDelegate($delegate)
{
    $this->aDelegates[] = $delegate;
}

The __call Method

public function __call($methodName, $parameters)
{

   $delegated = false;

   foreach ($this->aDelegates as $delegate)
   {
          if(class_exists(get_class($delegate)))
          {

         if(method_exists($delegate,$methodName))
         {
        $method     = new ReflectionMethod(get_class($delegate), $methodName);

        $function   = array($delegate, $methodName);

       return  call_user_func_array($function, $parameters);
          }
     }

   }

The __get Method

public function __get($property)
    {
        foreach($this->aDelegates as $delegate)
        {
            if ($delegate->$property !== false)
            {
                return $delegate->$property;
            }

        }


    }

All this works fine expect the function __set

public function __set($property,$value)
    {

        //print_r($this->aDelegates);

        foreach($this->aDelegates as $k=>$delegate)
        {
            //print_r($k);
            //print_r($delegate);
            if (property_exists($delegate, $property))
            {
                $delegate->$property = $value;
            }

        }
        //$this->addDelegate($delegate);
        print_r($this->aDelegates);


    }

class tester
{
  public function __set($name,$value)
    {
        self::$module->name(self::$name)->__set($name,$value);
    }
}


Module::test("logger")->log("test"); //  this logs,  it works
echo Module::test("logger")->path; //prints /home/bla/test/ this is also correct

But i cant set any value to class log like this

Module::tester("logger")->path ="/home/bla/test/log/";

The path property of class logger is public so its not a problem of protected or private property access.

How can i solve this issue? I hope i could explain my problem clear.

EDIT: A simple demonstration

Modules::Name("XML_Helper")->xmlVersion ="Hello"; // default is 333
$a =  Modules::Name("XML_Helper")->xmlVersion; // now $a should contain "Hello" 
echo $a; // prints 333

What i need is

Modules::Name("XML_Helper")->xmlVersion ="Hello"; // default is 333
$a =  Modules::Name("XML_Helper")->xmlVersion; // now $a should contain "Hello" 
echo $a; // prints Hello
+1  A: 

The path property of class logger is public so its not a problem of protected or private property access.

That's your problem. From the docs:

__set() is run when writing data to inaccessible properties.

That suggests that __set() is not called for public properties.

Frank Farmer
A: 

I realise you already said that path is public, but it's still worth mentioning: If you're using PHP 5.3.0+, note this quirk of property_exists():

5.3.0 | This function checks the existence of a property independent of accessibility

In other words, if you check if (property_exists($delegate, $property)), you have no guarantee you have access to $delegate->$property for writing (or reading, for that matter, but you are trying to write).

As for actual troubleshooting: You could try checking if your if (property_exists($delegate, $property)) statement actually executes. If it doesn't, check the case of $property.

Sidenote: It's fairly hard to read the code you posted up, which makes it a bit of a pain to troubleshoot. Could you edit your post and indent it properly?

pinkgothic