How to access an object from another class?

Here’s my current code inside the index method in my useListOption controller (for what it’s worth).

if ($_POST[‘job’] == ‘checkPrices’) {
$this->model->checkPrices();
}

It works satisfactorily, but I’d rather access the checkPrices object instead of the checkPrices method in the current class called useListOption.

How should I think about this?

Php gave me this Fatal error: Class declarations may not be nested in… when I tried to initiate the checkPrices class from inside the index method, useListOption class.

Thanks for helping me stretch my thinking.

But the code you just gave IS checkPrice and not priceCheck. What is the difference between the two?

1 Like

Thanks. My bad. I corrected the OP.

(too many years working in a grocery store)

1 Like

Ok. This makes more sense. Is the model class instantiating the checkPrice class? If it isn’t, then you would just call the checkPrice class like normal. If the model class is instantiating the checkPrice class, then you’d have to call that class by doing the same thing you are doing except this time, you are just calling a different method. So for instance, if the model class isn’t instantiating the checkPrice class, but the main controller is, then you would do something like

$this->checkPrice();

Which should return the checkPrice class object. If the model class is instantiating the checkPrice class, then you’d just do something like

$this->model->checkPrice();

If both checkPrice and checkPrices aren’t the same thing, then I am assuming that checkPrice is the class and checkPrices is the method that’s called within the model class.

Thanks spaceshiptrooper!

It was my Bootstrap that was throwing me off.
Problem solved:

require ‘controllers/checkPrices.php’;

class checkPrices extends Controller {
public function __construct() {
parent::__construct();
}
}

class useListOption extends Controller {
public function __construct() {
parent::__construct();
Session::init();
$logged = Session::get(‘loggedIn’);
if ($logged == false) {
Session::destroy();
header(‘location: …/login’);
exit;
}
}

public function index() {	
    	//if ($_POST['job'] == 'checkPrices') {
		$this->checkPrices();
	//}	
}

}

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.