PHP Classes and Objects: What do they mean to you?

 

If you are a relatively new web developer classes and objects are probably one of those topics you have heard about, but don’t really have a grasp on.  I know when I went to university very little time was spent on object orientation, which I really wished would have been covered.  The majority, if not all programming languages support objects.  It is a fundamental concept underlying the language.  You most likely use objects without even knowing it!.

First of all let’s ask why should we use objects?  Well the biggest reason is that it creates reusable code.  If you create a ShoppingCart class once, you can reuse it in many of your other projects.  How many times have you had to do a shopping cart?  The logic from project to project is exactly the same, so you could reuse the class you have already created to speed up your turnaround time.  Below I have shown how to create objects in a variety of languages:

PHP   – $mybook= new Book();
Ruby – mybook= Book.new
Python – mybook= Book()
Java – Book mybook= new Book();

You can see that the above statements all instantiate an object.  This means you use a class (blueprint) for which the basis of your object will be formed.  Objects will have properties, and methods associated with them.  For example let’s use an object of a book.  A book has attributes such as number of pages, author, and category.  These could be accessed in PHP using the following:

$mybook->numPages
$mybook->author

Methods (functions) on the other hand are actions taken.  So for example a book could close.  In PHP this is represented by:

$mybook->close();

Now these concepts are fine, but in order for the above to work we need to create a blueprint for the Book class.  Below is an example that maps the above logic:

class Book
{
    var numPages;
    var author;
    var closed;

    function __construct($author)
    {
        $this->closed = 0;
        $this->author = $author;
    }

    function close()
    {
        $this->closed = 1;
    }
}

We have introduced a few new things with the above code.  As you can see we define object properties by coding:

var propertyName;

We then create methods (functions) inside our object.  The __construct() function may look a bit strange.  It is a predefined function that PHP automatically loads when new Book() is called.  In this function we assign our properties values.  The $this variable is visible anywhere inside our class.  We tell our object to assign whatever author is passed into our function to our internal property.  Below is how we would give our book an author and how we could print it to the screen:

$mybook = new Book('CSS: The Missing Manual');
echo $mybook->author;

We have covered some of the basics of PHP objects.  The following are topics you can further research if you are interested!

  • Inheritance – Cobalt extends Car
  • Static properties and methods
  • Visibility – public, private, protected
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Add to favorites
  • Design Float
  • DZone
  • email
  • FriendFeed
  • PDF
  • Propeller
  • Reddit
  • RSS
  • StumbleUpon
  • Twitter

Related posts:

  1. Default and configuration objects with JavaScript
  2. Really Simple Model/View Seperation With PHP
  3. 6 Signs of Adaptive PHP
  4. Review of “PHP for Absolute Beginners”
  5. PHP CodeIgniter Validation


Written by Brenley Dueck

 

4 Responses to “PHP Classes and Objects: What do they mean to you?”

  1. Rob Desbois Says:

    October 28th, 2008 at 4:51 am

    Good introduction, but I’m not sure about your first point that “the biggest reason [to use objects] is that it creates reusable code”. This isn’t really the case – reusable code does not require object-oriented programming (OOP), and OOP does not imply reusable code.
    It is functions, not OOP, which provide code reuse.

    As I see it, the primary advantage of OOP is polymorphism which gives the ability to program to an interface.

    –rob

  2. rune_kg Says:

    October 28th, 2008 at 10:20 am

    Biggest benefit is to make fake namespaces :)

  3. iongion Says:

    October 28th, 2008 at 12:12 pm

    Another big advantage of OOP is encapsulation.

    But PHP is not really and Object oriented language, it has too many language constructs that you have no clue where they would fit in an OOP design (for example in oop regard, wth is array ? what is stdClass ? and more… needless to say about laking support for string objects or date (at least until recently))

    I think that long time ago, they preferred to just expose C extensions functions to a PHP counterpart … that is probably the main reason PHP feels so bloody dirty.

  4. admin Says:

    October 28th, 2008 at 12:33 pm

    I totally agree with you iongion and rob. Encapsulation and Polymorphism are huge benefits of Object Orientation. They are a bit more complex for a newbie to understand, so I didn’t talk about it to much.

    PHP is most commonly a procedural language, but PHP 5 has more support for classes and objects. Java is probably the best language that forces you to use classes and objects efficiently.

Leave a Reply

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

 
connect with me!