Spiga

Object Oriented Programing with PHP 5

Mayıs 23, 08 by aligorkem

From an object oriented perspective, we can describe most things as objects. An example of a typical object is a Student. A student will have been countless properties, variables and methods. These values describe to many properties, such as name, surname, id, school.

Setting Up Development Environments for the Object Oriented Programing with PHP 5
1.
Zend Development Environment (Zend Studio)
2. Apache Web Server (Includes Zend Studio) 
3. PHP 5 (Includes Zend Studio)

Contents
1. Properties
2. Methods
3. Basic Example : Student class
4.
Screenshots
5.
Constructor And Destructor
6.
Screenshots
7. Download Sample Code

Properties
We must create flexible student class and ask to "What properties do all students share in common" Because answers describe to properties for this class. For this example, we are going to say that all students name, surname, id and school name, etc.. 

Methods
Objects will send messages and take to some values from other objects. An instance of our student class will need to Going to School, Take an exam etc.. We have already These methods are those behaviors that our student can send.

Basic Example : Student class
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. We create new object from Student class.

$student = new Student();

Student.php

<?php
Class Student
{
    public  $name = "Jake";
    public  $surName = "Plummer";

    function TakeExam() {
       return ‘Failed’;
    }
}
?>

 

index.php

<?php
require_once "Student.php";

$student = new Student();
$name = $student->name;
$surName = $student->surName;
$examResult = $student->TakeExam();
?>

Name : <?= $name ?> <br>
SurName : <?= $surName ?> <br>
Exam Result : <?= $examResult ?>

 

Screenshots

 


 

We added a method to our class called TakeExam. Our class is now possible of doing something. We will have to create an instance of the class and define $student variable. We used the ‘new’ keyword to create an instance of the Student class.

$student = new Student();

We then called the TakeExam () method of the Student class. “$student” object is an instance of a Student class.

Constructor And Destructor
Constructors and destructors are special member functions of classes.Constructor created when object initialization. Destruction called when object cleaned. Constructors and destructors do not have return types nor can they return values. 

Constructor and destructors have special invisible parameters which are passed to them. These invisible parameters are used internally to instantiate the objects and classes. The way object initialization in PHP is that a special function, the constructor, is called when you instantiate an object.

Student.php

<?php
Class Student
{
    //Default value is empty
    private  $_name = "";
    private  $_surName = "";
    private  $_result = "";

    function  __construct( $name, $surName, $result)
    {
        //We set of the values
        $this->_name= $name;
        $this->_surName = $surName;
        $this->_result = $result;
    }
   
    function __destruct()
    {
    }
   
    function getName() {
       return $this->_name;
    }

    function getSurName() {
       return $this->_surName;
    }

    function TakeExam() {
       return $this->_result;
    }
}
?>

Example

     function  __construct( $name, $surName, $result)
    {
        $this->_name= $name;
        $this->_surName = $surName;
        $this->_result = $result;
    }

All required values set when object created. 

Index.php

<?php
    require_once "Student.php";
   
    $studentJake = new Student("Jake","Plummer","Failed");
    $name = $studentJake->getName();
    $surName = $studentJake->getSurName();
    $examResult = $studentJake->TakeExam();
   
    $studentJohn = new Student("John","Lennon","Succeeded");

?>

Name : <?= $name ?> <br>
SurName : <?= $surName ?> <br>
Exam Result : <?= $examResult ?> <br><br>

Name : <?= $studentJohn->getName() ?> <br>
SurName : <?= $studentJohn->getSurName() ?> <br>
Exam Result : <?= $studentJohn->TakeExam() ?>

Example
We passed 3 value to constructor methods of Student class.

$studentJake = new Student("Jake","Plummer","Failed");

Screenshots

 

DOWNLOAD SAMPLE CODE

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • description
  • eKudos
  • E-mail this story to a friend!

This entry no have comments... but you can be first.

Leave a Reply