PostgreSQL & PHP Tutorials - Extending Your Classes

PHP »  Introduction to Object Oriented Programming

Posted By Chris Smith Posted on 19 Feb 2006, 07:13 AM
Viewing page 2 of 5
« | Back | Next | »

As with other programming languages, you can easily extend classes and objects:


<?php
class {

    function 
__construct() {
        
// this gets called in php5.
        
echo "I am in function " __FUNCTION__ " of class " __CLASS__ "<br/>";
    }

    function 
a() {
        
// this gets called in php4.
        // we'll have to call the constructor ourselves.
        
$this->__construct();
    }
}

class 
extends {
}

$my_class = new b();

?>


The 'extends' keyword means 'class b' inherits all of 'class a' properties and methods (methods are simply 'functions' inside a class).

We haven't actually defined anything in 'class b' yet - so php will go back to 'class a' to see what we need to do.

If you run this through your browser you will see:

I am in function __construct of class a
Viewing page 2 of 5
« | Back | Next | »



Avg Rating: 3
Vote Count: 27


              

  New Reply


I just have a comment/question. Why dont you implement abstract classes into this example rather than saying you cannot access update/delete etc. from the API class. I would make them abstract, explain a bit about what it is.

[ Editor's note - Interested in submitting an article about it? ]
Joseph Crawford 09 Jun 2006 Reply

This tutorial is very nice, I am trying to read up on creating my own API so the way you have structured things has helped a lot. Carlton Dickson 18 Jul 2007 Reply


Want to post a comment? Fill in the details below.

Your Name  : 
Your Email  : 
Your Website  : 
Spam Check! Please answer this question  : 4 + 6 =
Comment