You are on page 1of 15

F5224 WEB PROGRAMMING

CLASSES AND OBJECTS

Prepared by : Munirah Abd

WHAT IS A CLASS?

Conceptually, a class represents an object, with associated methods and variables

CLASS DEFINITION
<?php class myClass { Creating an object //code will go here } $object1= new myClass(); echo \$object1 is an .gettype($object1); ?> //output $object1 is an object Get the type of a variable

PROPERTIES OF OBJECTS
The variable declared inside an object are called properties Properties can be values ,arrays or even other objects
<?php class var var var } ?>

myCar { $color = silver; $make = Mazda; $model = Protege5;

Properties

HOW TO ACCESS THE PROPERTIES


<?php class myCar { var $color = silver; var $make = Mazda; var $model = Protege5; } $car = new MyCar(); echo I drive a .$car->color. .$car->make. .$car->model; ?> //Output I drive a silver Mazda Protege5.

CHANGGING OBJECT PROPERTIES


<?php class myCar { var $color = silver; var $make = Mazda; var $model = Protege5; } $car = new MyCar(); $car -> color =red; $car -> make = Porsche; $car -> model = Boxter; echo I drive a .$car->color. .$car->make. .$car->model; ?> //Output I drive a red Porsche Boxter

OBJECT METHODS
A class with a method
<?php class myClass{ function sayHello(){ echo HELLO!; } }

//output HELLO!
Object method

$object1 = new myClass(); $object 1 -> sayHello();


?>
-> operator is used to call the object method

OBJECT METHODS
Accessing class properties within a method
<?php class myClass{ var $name = MATT; function sayHello(){ echo HELLO! My name is .$this->name; } //output } $object1 = new myClass(); $object 1 -> sayHello(); ?>

HELLO! My name is MATT

OBJECT METHODS
$this Is used to refer to the currently instantiated object An object refers to itself, and you must use the $this variable

Using the $this variable in conjunction with the -> operators in enables you to access any property or method in a class,within the class itself.

OBJECT METHODS
Changing the value of a property from within a method
<?php //output class myClass{ var $name = MATT; function setName($n){ HELLO! My name is Julie $this->name = $n; } function sayHello(){ echo HELLO! My name is .$this->name; } } $object1 = new myClass(); $object1 -> setName(Julie); $object1 -> sayHello(); ?>

CONSTRUCTORS
In PHP4 constructor is a function which has the same name as a class. So, if you have a class named MyClass, constructor is a function named MyClass.
In PHP5 constructor is a function called __construct. PHP5 is a backward compatible, so if it can not find a function named __construct in a class declaration, it will search for a PHP4 style constructor (function with a name same as a class) and use it as a constructor.

CONSTRUCTORS
Example PHP5
<?php class Person4 { var $name; var $address; var $phone; // this is constructor function Person4() { $this->name = "John Doe"; $this->address = "Unknown"; $this->phone = "Unknown"; } function printPersonInfo() { echo $this->name . "\n"; echo $this->address . "\n"; echo $this->phone . "\n"; } } $obj = new Person4(); $obj -> printPersonInfo(); ?>

CONSTRUCTORS
Example PHP5
<?php class Person5 { var $name; var $address; var $phone; // this is constructor function __construct() { $this->name = "John Doe"; $this->address = "Unknown"; $this->phone = "Unknown"; } function printPersonInfo() { echo $this->name . "\n"; echo $this->address . "\n"; echo $this->phone . "\n"; } }$obj = new Person4(); $obj -> printPersonInfo(); ?>

CONSTRUCTORS
Example PHP5
<?php class book { private $title; private $isbn; private $copies; public function __construct($isbn) { $this->setIsbn($isbn); $this->getTitle(); $this->getNumberCopies(); } public function setIsbn($isbn) { $this->isbn = $isbn; } public function getTitle() { $this->title = "title"; print "Title: " . $this->title . "<br />"; } public function getNumberCopies() { $this->copies = "5"; print "Number copies available: " . $this->copies."<br />"; } } $book = new book("1111"); ?>

THE END