Michał Lipa
Michał Lipa

Reputation: 978

Getting access to constructor variable in Codeigniter

How I can get access and work on variable from constructor?

When I initialize my function hello I get error:

Message: Undefined variable: type2

Library:

class MyLibrary {


public function __construct($params)
{
    echo $params['type'];

    //I WANT TO DISPLAY TYPE2 BY HELLO FUNCTION
    $type2 = 'asdasdasd';
}

public function hello(){

    return $type2;
}
}

My controller:

  public function test()
{
    $params = array('type' => 'large', 'color' => 'red');
    $this->load->library('TwitterAPI', $params);

     //I WANT TO DISPLAY $TYPE2 
     echo $this->twitterapi->hello();

}

Upvotes: 0

Views: 1912

Answers (1)

Steve
Steve

Reputation: 20469

Create a private member variable:

class MyLibrary {

    private $type2;

    public function __construct($params)
    {
        echo $params['type'];

        //I WANT TO DISPLAY TYPE2 BY HELLO FUNCTION
        $this->type2 = 'asdasdasd';
    }

    public function hello()
    {

        return $this->type2;
    }

}

Upvotes: 2

Related Questions