0

I have initialized a new instance of a library in the __construct(){} method of a PHP class and equated it to a variable,

but now I want to use this variable to access methods of the library inside another function but PHP is not allowing me do that.

class Demo 
{

public function __construct()
    {

    parent::__construct(new PaymentModel);

    $this->api = new Api(config("razorpay", "key_id"), config("razorpay", "key_secret"));

    }

public function createOrder()
  {
    $order = $api->order->create();
    echo '<pre>';
    var_dump($order); die;

  }
}

I looked at the __construct documentation and some other answers here on stack overflow but all they did was confuse me more than helping me out.

Please help me figure this out as I am a starter myself in tech.

1
  • Add $this to api in your createOrder method Commented Oct 26, 2018 at 9:46

5 Answers 5

1

To be able to use $this->api in your class, you will need to set it as an attribute.

so :

class Demo 
{
private $api;

public function __construct()
    {

    parent::__construct(new PaymentModel);

    $this->api = new Api(config("razorpay", "key_id"), config("razorpay", "key_secret"));

    }

public function createOrder()
  {
    $order = $this->api->order->create();
    echo '<pre>';
    var_dump($order); die;

  }
}

Also, as notified by other, you are to construct a parent class while your class 'Demo' does not extend any other class.

Sign up to request clarification or add additional context in comments.

Comments

0

You have defined api as a class variable (property). Use $this->api to acces this class variable (property) in other methods of your class.

// This class probably inherits some base class
class Demo extends BaseDemo 
{

public function __construct()
    {

    parent::__construct(new PaymentModel);

    $this->api = new Api(config("razorpay", "key_id"), config("razorpay", "key_secret"));

    }

public function createOrder()
  {
    $order = $this->api->order->create();
    echo '<pre>';
    var_dump($order); die;

  }
}

Also check your class definition - if parent::__construct() is called, then your class probably inherits some base class. If this is not the case, remove parent::__construct() call.

1 Comment

Yes, it does inherit a base class. I just removed all the non-useful information from the question so that I can target only the problem at hand. Your answer was helpful. Thanks
0

To access object variables you need to use $this. In your case change the first line in createOrder() to $order = $this->api->order->create();

Also you appear to be trying to run the classes parents constructor in your constructor. But the class doesn't have a parent.

Comments

0

you are calling parent::__construct(new PaymentModel); on a class which doeasn't extend any base class.

Comments

0

declare variable $api in a class then only accessible to other function body

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.