Jump to content

Confused With Classes


next

Recommended Posts

I dropped PHP right as I started learning OOP, so my knowledge in it is very limited. However I'm trying to understand opencart, but it's a little confusing to me. Currently I'm looking at USPS functionality and can't figure out where all these methods and properties come from. Here's a snippet of USPS class:

class ModelShippingUsps extends Model {
public function getQuote($address) {
 $this->load->language('shipping/usps');


 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('usps_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");


 if (!$this->config->get('usps_geo_zone_id')) {
  $status = true;
 } elseif ($query->num_rows) {
  $status = true;
 } else {
  $status = false;
 }

So, in the above, $this is basically ModelShippingUsps, if I remember correctly... ModelShippingUsps is an extension of Model and it inherits Model's methods and properties.

This is what Model looks like:

abstract class Model {
protected $registry;

public function __construct($registry) {
 $this->registry = $registry;
}

public function __get($key) {
 return $this->registry->get($key);
}

public function __set($key, $value) {
 $this->registry->set($key, $value);
}
}

 

Where do config->get() and db->query come from? How does ModelShippingUsps has access to them?

There's so much code and so little documentation on this cart, it's not even funny :(

Link to comment
Share on other sites

the __get and __set methods defined in the Model class are known as magic methods.  Basically PHP will automagically call them under certain situations.  For __get and __set, they get called whenever you attempt to read the value of, or set the value of, a property that does not exist.

 

So when the ModelShippingUsps class attempts to access $this->config, PHP realizes there is no accessible config property on either ModelShippingUsps or Model so it attempts to run $this->__get('config') instead which causes the __get method in the Model class to be executed.

 

The get method will attempt to find a value in the $registry collection with the name of the missing property ('config' in this case and return whatever it finds.  In this case there must be some sort of configuration object which has a get() method defined within it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.