Jump to content

Archived

This topic is now archived and is closed to further replies.

avo

OOP with php

Recommended Posts

Hi All

Is there anyone willing to show/teach me this subject via voice chat and pc control please let me know you rate per hour
any country wellcome as long as you would be able to fit in with uk times

im ok at php and understand what is going on but i would like to take it to the next level now

thanks in advance.

Share this post


Link to post
Share on other sites
Have a look at this:

http://www.webreference.com/programming/phpanth2/

Share this post


Link to post
Share on other sites
A simple introduction to OOP and PHP

[size=12pt][b]Classes and objects[/b][/size]

An object is a variable of a specific type. The class is the definition of that type.

[b]Encapsulation[/b]

A class definition defines properties (variables) and methods (functions) which operate on those properties. The data and the processes on the data are encapsulated in the class definition.

When you create an object, it is called an "instance" of the class. Several objects of the same class may have different values for their properties, but the methods are the same for all objects of that class.

Example class definition
[code]<?php
/***************************
** define base 'pet' class
****************************/
class pet {
      protected $speech;

      function __construct () {
              $this->speech = '****';
      }

      function talk () {
              echo "<p>$this->speech</p>";
      }
}
?>[/code]

This class has a property "speech" and a "talk" method using the speech property.

The speech property is defined as "protected" which means its value is also accessible by any subclasses (more of which later). It could also be defined as "private", which limits access to methods within the same class, or "public", allowing access from outside the classes or subclasses. (PHP4 uses only "var" to define a property.)

The __construct() method is called when the object is created and is used to initialise property values. (PHP4 - define a function with the same name as the class instead.)

"$this->" is used to reference its own properties or methods (eg set the speech property of this object to '****')

To create a pet object and make it talk

[code]<?php
$pet = new pet; // creates a pet object
$pet->talk();  // --> ****
?>[/code]

[b]Inheritance[/b]

Now we define 2 subclasses of the pet class, namely cat and dog classes.

[code]<?php
/***************************
** define 'dog' subclass
****************************/
class dog extends pet {
      function __construct () {
              $this->speech = "Woof!";
      }
}

/***************************
** define 'cat' subclass
****************************/
class cat extends pet {
      function __construct () {
              $this->speech = "Meeow!";
      }
}
?>[/code]

Note that we did not define any properties or talk function. Because the parent class already contains these, they are inherited by the subclasses. So to create a dog object and make it talk

[code]<?php
$pet = new dog;
$pet->talk();  // --> Woof!
?>[/code]

Next, we want to create NoisyDog class. This will be a type of dog but its speech method is going to be different from that of our dog. To do this, all we will define is its own talk() method to "override" its parent method.

[code]<?php
/***************************
** define 'noisydog' subclass
****************************/
class noisydog extends dog {
      function talk () {
            for ($i=0; $i<10; $i++)
                echo "$this->speech ";
      }
}
?>[/code]

[b]Putting it all together[/b]

[code]
<?php

/***************************
** define base 'pet' class
****************************/
class pet {
      var $speech;

      function __construct () {
              $this->speech = '****';
      }

      function talk () {
              echo "<p>$this->speech</p>";
      }
}

/***************************
** define 'dog' subclass
****************************/
class dog extends pet {
      function __construct () {
              $this->speech = "Woof!";
      }
}

/***************************
** define 'cat' subclass
****************************/
class cat extends pet {
      function __construct () {
              $this->speech = "Meeow!";
      }
}

/***************************
** define 'noisydog' subclass
****************************/
class noisydog extends dog {
      function talk () {
            for ($i=0; $i<10; $i++)
                echo "$this->speech ";
      }
}

//------------ END CLASS DEFINITIONS ----------

/***************************
** process the form data
****************************/
if (isset($_GET['pet'])) {

    /*********************
    ** create pet object
    **********************/
    switch ($_GET['pet']) {
            case 'cat': $mypet = new cat; break;
            case 'dog': $mypet = new dog; break;
            case 'noisy': $mypet = new noisydog; break;
            default: $mypet = new pet; break;
    }

    /*********************
    ** tell it to talk
    **********************/
    $mypet->talk();

}
//-------------------------------------------
?>
<FORM>
<INPUT TYPE='RADIO'  name='pet' value='cat' checked> Cat<br>
<INPUT TYPE='RADIO'  name='pet' value='dog'> Dog<br>
<INPUT TYPE='RADIO'  name='pet' value='noisy'> Noisy Dog<br>
<INPUT TYPE='RADIO'  name='pet' value=''> Something else<br>
<INPUT TYPE='SUBMIT'  name='submit' value='Talk to me'>
</FORM>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.