Magic method __set () for an object initiated

Ok I have a problem, sorry if I can't explain it clearly, but the code speaks for itself.

I have a class that generates objects from a given class name; Let's say we say a 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;
 }

      

AddDelegate method:

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

      

__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);
          }
     }

   }

      

__Get method

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

        }


    }

      

This all works fine, waiting for the __set function

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 cannot set any value in the class log like this

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

      

The path property for the class registrar is public, so its issue is not related to accessing protected or private property.

How can I solve this problem? Hopefully I can explain my problem.

EDIT: Simple demo

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

      

I need

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

      

+2


a source to share


2 answers


I understand you already said the path is public , but it's still worth mentioning: if you're using PHP 5.3.0+, note this quirk property_exists()

:

5.3.0 | This function checks for the existence of an accessibility-independent property

In other words, if you checked if (property_exists($delegate, $property))

, you have no guarantee that you have $delegate->$property

write access (or read access , for that matter, but you are trying to write).



As for the actual troubleshooting: you can check if your statement is actually being executed if (property_exists($delegate, $property))

. If not, check case $property

.

Sidenote: It's quite difficult to read the code you've posted, making it difficult to troubleshoot. Could you edit your post and indentation correctly?

+1


a source


The path property for the class registrar is public, so its issue is not a protected or private property issue.

It's your problem. From the docs :



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

      

This says it is __set()

not called for public properties.

+1


a source







All Articles