Jump to content


Photo

OOP with php


  • Please log in to reply
2 replies to this topic

#1 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 19 August 2006 - 06:25 PM

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.
Im loving it ........

#2 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 19 August 2006 - 07:43 PM

Have a look at this:

http://www.webrefere...mming/phpanth2/
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 August 2006 - 10:12 PM

A simple introduction to OOP and PHP

Classes and objects

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

Encapsulation

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
<?php
/***************************
** define base 'pet' class
****************************/
class pet {
      protected $speech;

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

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

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

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

Inheritance

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

<?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!";
      }
}
?>

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

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

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.

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

Putting it all together

<?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>


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users