next Posted December 15, 2012 Share Posted December 15, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272020-confused-with-classes/ Share on other sites More sharing options...
kicken Posted December 15, 2012 Share Posted December 15, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272020-confused-with-classes/#findComment-1399491 Share on other sites More sharing options...
next Posted December 15, 2012 Author Share Posted December 15, 2012 Nice explanation, thank you Quote Link to comment https://forums.phpfreaks.com/topic/272020-confused-with-classes/#findComment-1399497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.