Problem Array to String Conversion

0

I have a class for Codeigniter with a property $keywords that I want to assign with the function set_keywords()

class Seo
{

    private $keywords = array();


    public function set_keywords($keywords)
    {
        $this->$keywords = $keywords;
    }
}

And in my controller:

public function index()
{
    $this->load->library('seo');
    $keywords = array('hola','mundo');
    $this->seo->set_keywords($keywords);

    $this->load->view('inicio_view');
}

I get the error

  

Array to string conversion

And I have no idea what the problem is.

    
asked by Juan Luis 04.01.2018 в 04:29
source

2 answers

1

The possible error that I find in your code that does not have to do only with Codeigniter if not with PHP in general and with POO ,

You are trying to access the attribute incorrectly, since to access the property, the name of the property should be without the $ sign, since this $ sign is to represent variables in PHP .

class Seo
{
    private $keywords = array();
    public function set_keywords($keywords)
    {
        $this->keywords = $keywords;
    }
}
    
answered by 04.01.2018 / 05:57
source
0

Your code is clear, do you have the code of the chance view ?, try to print it in the seo library

class Seo{

private $keywords = array();


public function set_keywords($keywords)
{
    $this->$keywords = $keywords;
    var_dump($this->$keywords);
    die;


}

and comment if the variable passes and as it prints, the problem may be that you are not passing the data to the view in the controller should be something like

$datos['keywords'] = $this->seo->set_keywords($keywords); $this->load->view('inicio_view',$datos); thinking that what your library does is data manipulation

    
answered by 04.01.2018 в 06:08