A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Object-oriented programming PHP 5 Tutorial

For those who are not familiar with PHP5 OOP, I found this: HERE

There are many benefits of inheritance with PHP, the most common is simplifying and reducing instances of redundant code. Class inheritance may sound complicated, but think of it this way. Consider a tree. A tree is made up of many parts, such as the roots that reside in the ground, the trunk, bark, branches, leaves, etc. Essentially inheritance is a connection between a child and its parent.Creating a parent object

Let’s say that our intention is to create a storefront where we’ll be selling cars. A simple shopping cart allows us to sell the cars and allows consumers to browse the products, and access specific information such as pricing and a description. With this in mind we know that regardless of what we’re selling, all products have certain things in common, such as a name, description, price and photo. By inheriting the Product object we can share these common properties across unique child objects.

class Product
   private $name;
   private $price;
   private $photo;
   private $description;public function Product() {}
   protected function setName($name) { $this->name = $name; }
   public function GetName() { return $this->name; }
   protected function setPrice($price = '0.00') { $this->price = $price; }
   public function GetPrice() { return $this->price; }
   protected function setPhoto($photo) { $this->photo = $photo; }
   public function GetPhoto() { return $this->photo; }
   protected function setDescription($description) { $this->description = $description; }
   public function GetDescription() { return $this->description; }

The Product object is quite simple, it allows us to define specific information such as a name, price, photo and description. The objects setters allow us to set these specific product properties, and by using the protected keyword we only allow these properties to be set by extending child objects. Also, the Products getters are public, which allows us to obtain specific data from an object with any PHP page that includes and instantiates it. The last thing to note about this object are the properties defined at the beginning of the class. We define these as private properties so they can’t be changed directly unless using the setters provided by the Product object, this provides more security and reliability. Let’s look at how to inherit the Product object.

This entry was posted in Uncategorized and tagged . Bookmark the permalink.

One Response to Object-oriented programming PHP 5 Tutorial

  1. Pingback: nookli » Blog Archive » Object-oriented programming PHP 5 Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>