Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes in PHP Web Engineering. What is Class? A class is a collection of variables and functions working with these variables. Variables are defined.

Similar presentations


Presentation on theme: "Classes in PHP Web Engineering. What is Class? A class is a collection of variables and functions working with these variables. Variables are defined."— Presentation transcript:

1 Classes in PHP Web Engineering

2 What is Class? A class is a collection of variables and functions working with these variables. Variables are defined by var and functions by function. A class is defined using the following syntax: <?php class test { function test123() { echo “OK”; } } ?>

3 Class In PHP Concept of class ( or basic object oriented structure) introduced from php4. But complete coverage of class like access modifier or interface is introduced from php5. Creating class is very easy in php. You can create class with help of using class keywordin php.

4 Example – Define Class class myOwnClass { //variables or properties of the class var $variable1; var $variable2; //Function of class function mergeVariable() { return $this->variable1. $this->variable2; } } $this variable is used to make the object can get information about itself. "$this->variable1" indicates / makes to be called the “variable1" property of the current object of this class. When accessing properties, you need only one $. The syntax is $obj->property

5 What is Object Classes are useless without objects. Object is an instance of your class. If you have class then you need to create object of the class to solve your problem using class. You can create object of your class by using new keyword. $objClass = new myOwnClass; OR $objClass = new myOwnClass(); NOTE: You can create an object of class with or without ().

6 Example class MyLock { var $isLocked = false; function unlock() { $this->isLocked = false; echo 'You unlocked the Lock'; } function lock() { $this->isLocked = true; echo 'You locked the Lock'; } $mylockobj= new MyLock; $mylockobj->unlock(); // You unlocked the Lock (Call Function of Class)

7 Constructor Function The Constructor method is a special type of function called __construct within the class body. To declare /create a constructor method, use the __construct name (begins with two underscore "__"). This method is always "public" even if this attribute is not specified. The difference from the other functions is that a constructor method is automatically invoked when an object is created. NOTE: if you have __construct then it will be the first preference. If __construct function is not present then it will search for the function with the same name of class.

8 <?php class MyDestructableClass { Var $name; function __construct() { print "In constructor\n"; $this->name = “I m in constructor"; } function __destruct() { print "Destroying ". $this->name. "\n"; } } $obj = new MyDestructableClass; ?>

9 class Door { private $lock; private $connectsTo; public function __construct() { $this->lock = “Yes”; $this->connectsTo = 'bedroom'; } public function open() { Echo “Your door lock value is “.$this->lock; echo 'You opened the Door : ', $this->connectsTo; } $myDoor = new Door(); $myDoor->open();


Download ppt "Classes in PHP Web Engineering. What is Class? A class is a collection of variables and functions working with these variables. Variables are defined."

Similar presentations


Ads by Google