Class and Objects
Class and Objects
WHAT IS A CLASS?
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 } ?>
Properties
OBJECT METHODS
A class with a method
<?php class myClass{ function sayHello(){ echo HELLO!; } }
//output HELLO!
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(); ?>
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