Note. Convert Array to String - Why?

Hi, I am trying to execute the following PHP code, however im getting an error. I pass the link to the main class, which I want to assign to a variable within the class.

Note. Convert array to string

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;      
   }  
}
+3
source share
4 answers

Ok, first ...

$this->$config

Second $to $configbe removed, since otherwise it tries to access the variable name specified in string $config. (for example, if $ config holds "test"a value, you will receive access to the variable "test"in its class $this->test)

$config, , ? (String, , ..?)

+12

private $config = array();

+1

$this->config = $config;

+1
source

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();
0
source

Source: https://habr.com/ru/post/1757692/


All Articles