Question

Hi im trying to execute the following PHP code, however im receiving an error. Im passing a reference into the core class, which i want to assign to a variable within the classes scope..

Notice: Array to string conversion

Thanks in advance..

$core = new core($config);
$core->execute();   

class core
{
   private $config;

   public function __construct(&$config)
   {
      $this->$config = $config;
   }

   public function execute()
   {
      $this->set_path();
   }

   private function set_path()
   {
      return true;      
   }  
}
Was it helpful?

Solution

Well, first off....

$this->$config

The second $ in $config should be removed since otherwise it's trying to access the variable with the name given by the string within $config. (e.g., if $config held "test" as a value, you'd be accessing the "test" variable within your class: $this->test)

What is $config when it is passed in, anyway? (String, array, object, etc?)

OTHER TIPS

private $config = array();

$this->config = $config;

This works without errors in php 5.2.
What version of php are you using?

<?php
class core
{
   private $config;

   public function __construct(&$config)
   {
      $this->config = $config;
   }

   public function execute()
   {
      $this->set_path();
   }

   private function set_path()
   {
      return true;      
   }  
}

$config=array(
     'a'    => '1'
    ,'b'    => '2'
    );

$core = new core($config);
$core->execute();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top