This article will offer a basic understanding of Classes, what they are and how they relate to the Opencart framework. Perhaps this knowledge would be beneficial to those that just want a basic understanding of the Opencart “core”, or for developers to extend development off of the core to code out extensions/plugins, or customization.
What is a class?
A class is a collection of variables and functions working with these variables. Variables are defined by var and functions by function. Classes and Object Oriented Language has been refereed to as Satan, and extremely complicated but really is very simple if you understand the basic privacy levels, rules, and how to use them. Today we’re going to be modeling a Poplar tree and an Oak tree for the entire tutorial as classes, will be assigning a few attributes to them and varying privacy levels. Poplar and Oak tree’s are actually pretty similar – a lot more similar than people think, but there are some crucial differences. Some of the shared qualities are things like bark texture, leaf colour, height and weight. But there are a couple of glaring differences between the two such as root structure, density of the grain, and burn temperature.
The way that inheritance works within classes is that you put as many common items within one class and then extend that parent class for each sub-class. Sound complicated? Let me clarify, we have a class of Tree that will be extended to create Oak and Poplar. There will be some specifics that we set by default when we create a new Oak or Poplar tree.
The code in today’s article is taken from Opencart 2.0. I”ll be giving you code example from the custom Tree class as well as Opencart at the same time.
To successfully run an action on initialization of a class, we look to add a constructor, this means that when you create a new Oak Tree using new Oak()
the constructor will run and we can set the tree specific attributes:
<code class="php"><span class="class"><span class="keyword">class</span> <span class="title">Tree</span> {</span> <span class="keyword">private</span> <span class="variable">$alive</span> = <span class="keyword">true</span>; <span class="keyword">protected</span> <span class="variable">$type</span> = <span class="string">''</span>; <span class="keyword">protected</span> <span class="variable">$height</span> = <span class="string">''</span>; } <span class="class"><span class="keyword">class</span> <span class="title">Oak</span> <span class="keyword">extends</span> <span class="title">Tree</span> {</span> <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">()</span>{</span> <span class="variable">$this</span>->type = <span class="string">'red'</span>; <span class="variable">$this</span>->height = 80; } } </code>
This is taken from Opencart in /system/library/cart.php. It gives you an example of the construct in the cart class
class Cart { private $config; private $db; private $data = array(); public function __construct($registry) { $this->config = $registry->get('config'); $this->customer = $registry->get('customer'); $this->session = $registry->get('session'); $this->db = $registry->get('db'); $this->tax = $registry->get('tax'); $this->weight = $registry->get('weight'); if (!isset($this->session->data['cart']) || !is_array($this->session->data['cart'])) { $this->session->data['cart'] = array(); } }
In the class Tree example, we have used the word extends
to show that Oak
is an extension of the Tree
class. You can imagine this as the variables within Tree
being copied to a new class called Oak
so they are available to use if their privacy allows.
Privacy
What you’ll see in the above code is words such as protected
, public
and private
, these are varying levels of privacy and dictate where these attributes can be altered, or functions called:
- Public – Can be called anywhere, outside of the class, inside the class, or even in a child.
- Protected – Can be called from only within the current class or any children of that class (eg:
type
can be altered byTree
orOak
) - Private – Can only be called by the containing class. (eg:
size
can only be altered byTree
)
$this, parent, self, static
Above, you’ll see reference to $this
, this just refers to the current instance of the object that you’re in, $this
can and does creep up the chain, so I can access inherited variables using $this
as above.
If I had overwritten a method in a child class, I can call for the parent method by using parent::method()
, so for example if we had a constructor within the Tree
class we would call that from our Oak
class by using parent::__construct()
<code class="php"><span class="class"><span class="keyword">class</span> <span class="title">MySite</span> {</span> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">TheTime</span><span class="params">()</span>{</span> <span class="keyword">return</span> <span class="string">'Tomorrow'</span>; } } MySite::TheTime() <span class="comment">/* If we chose to instantiate the class we'd do $site = new MySite; $site->TheTime(); */</span></code>
Magic Methods
These are at times referred to as Overloading. This is the instance by where you can have a generic function that is called if you try to get or set a variable that doesn’t exist, or try to call a function that doesn’t exist. There are 2 that I’ll run through, and they’re all very similar they are __get()
and __set()
. Make sure that you declare both of these as public.
Set and get
If we try to set or get a variable that doesn’t exist, inherently PHP will throw an error, here we can gracefully handle that by actually using a method to handle that. This is very useful if – as with active record methodology – variables are stored within an array called data
in the class:
<code class="php"><span class="class"><span class="keyword">class</span> <span class="title">TodaysExample</span> {</span> <span class="keyword">private</span> <span class="variable">$data</span> = <span class="keyword">array</span>(); <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__set</span><span class="params">(<span class="variable">$variable</span>, <span class="variable">$value</span>)</span>{</span> <span class="keyword">echo</span> <span class="string">'Setting '</span> . <span class="variable">$variable</span> . <span class="string">' to '</span> . <span class="variable">$value</span>; <span class="variable">$this</span>->data[<span class="variable">$variable</span>] = <span class="variable">$value</span>; } <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__get</span><span class="params">(<span class="variable">$variable</span>)</span>{</span> <span class="keyword">if</span>(<span class="keyword">isset</span>(<span class="variable">$this</span>->data[<span class="variable">$variable</span>])){ <span class="keyword">return</span> <span class="variable">$this</span>->data[<span class="variable">$variable</span>]; }<span class="keyword">else</span>{ <span class="keyword">die</span>(<span class="string">'Unknown variable.'</span>); } } } <span class="variable">$t</span> = <span class="keyword">new</span> TodaysExample; <span class="comment">// Set a non-existent variable</span> <span class="variable">$t</span>->test = <span class="string">'Test Variable'</span>; <span class="comment">// Get what we just stored</span> <span class="keyword">echo</span> <span class="variable">$t</span>->test; <span class="comment">// Get a non-existant variable</span> <span class="keyword">echo</span> <span class="variable">$t</span>->testFail; </code>
Singletons
A singleton class is a class that should really only be instantiated once and never instantiated again, period.
<code class="php"><span class="class"><span class="keyword">class</span> <span class="title">ExampleSingleton</span> {</span> <span class="keyword">private</span> <span class="keyword">static</span> <span class="variable">$instance</span> = <span class="keyword">NULL</span>; <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getMyInstance</span><span class="params">()</span>{</span> <span class="keyword">if</span>(is_null(<span class="keyword">self</span>::<span class="variable">$instance</span>)){ <span class="keyword">self</span>::<span class="variable">$instance</span> = <span class="keyword">new</span> <span class="keyword">self</span>(); } <span class="keyword">return</span> <span class="keyword">self</span>::<span class="variable">$instance</span>; } } <span class="variable">$class</span> = ExampleSingleton::getMyInstance();</code>
Opencart Class Example
Here is an example of the Opencart class called cart found in system/library/cart.php. The important thing here to remember is how the session cart is created within the construct every time the class is loaded.
class Cart { private $config; private $db; private $data = array(); public function __construct($registry) { $this->config = $registry->get('config'); $this->customer = $registry->get('customer'); $this->session = $registry->get('session'); $this->db = $registry->get('db'); $this->tax = $registry->get('tax'); $this->weight = $registry->get('weight'); if (!isset($this->session->data['cart']) || !is_array($this->session->data['cart'])) { $this->session->data['cart'] = array(); } }
Looking for quality OpenCart Web Hosting? Look no further than Arvixe Web Hosting!