I am trying to understand php classes. I'm attempting to make a new class in opencart to centralise some custom queries. In opencart you have models for admin and models for catalog but you cannot use the same model for both...i believe? So i was thinking a new class may do it? My Class:
class Ship {
public $product_id = null;
public function __construct($registry) {
$this->db = $registry->get('db');
$this->product_id = $product_id;
}
public function getProductDimensions($product_id) {
$query = $this->db->query("SELECT `length`,`width`,`height`,`length_class_id` FROM " . DB_PREFIX . "product WHERE product_id='" . (int)$product_id . "'");
return $query->row;
}
}
And i'm calling the class function like this:
$product_size = $this->ship->getProductDimensions('872');
I get an error: Fatal error: Uncaught Error: Call to a member function getProductDimensions() on null
I used the cart class as a guide and $this->cart->add(variables) works from catalog/controllers , but because i dont fully understand classes, i must be missing something.
Can anyone help me to understand(now thats a question), what i am missing?
Ok, after some hours, i found the answer.
I needed to load the class, i thought they loaded automatically at startup?
Hey ho, now i can carry on adding stuff.