Object Oriented Programing with PHP 5
Mayıs 23, 08 by aligorkemFrom 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.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 then called the TakeExam () method of the Student class. “$student” object is an instance of a Student class.
Constructor And Destructor
Constructor and destructors have special invisible parameters which are passed to them. These invisible parameters are used internally to instantiate the objects and classes.
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
{
$this->_name= $name;
$this->_surName = $surName;
$this->_result = $result;
}
Index.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() ?>












