Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Stands for "Object-Oriented Programming." OOP refers to a programming methodology based on objects, instead of just functions and procedures. These objects.

Similar presentations


Presentation on theme: " Stands for "Object-Oriented Programming." OOP refers to a programming methodology based on objects, instead of just functions and procedures. These objects."— Presentation transcript:

1  Stands for "Object-Oriented Programming." OOP refers to a programming methodology based on objects, instead of just functions and procedures. These objects are organized into classes, which allow individual objects to be group together. Most modern programming languages including Java, C/C++, and PHP, are object-oriented languagesJavaC/C++PHP

2  An "object“ is a "instance," of a class. An object can also call functions, or methods, specific to that object.  Classes are the (partial) definition of an object. Also referred to as a “object blueprint”, the class defines how the object will look after first instantiation, and how it will behave in response to operating on it

3  Encapsulation  Inheritance  Polymorphism

4  Encapsulation enables you to hide, inside the object, both the data fields and the methods that act on that data. Public Private Protected

5  We can create a hierarchical relationship between classes and derived subclasses. A subclass inherits properties and methods from its super class.

6  It is characteristic using which we can assign a single name to multiple functions. To differentiate functions either we can change the argument type or number of arguments.  Overloading  Overriding

7 class Dog { public $hungry = 'hell yeah.'; function eat($food) { $this->hungry = 'not so much.'; } $dog = new Dog; echo $dog->hungry; $dog->eat('cookie'); echo $dog->hungry;

8 class Animal { public $hungry = 'hell yeah.'; function eat($food) { $this->hungry = 'not so much.'; } class Dog extends Animal { function eat($food) { if($food == 'cookie') { $this->hungry = 'not so much.'; } else { echo 'barf, I only like cookies!'; }

9  Constructors are a way to define the default behaviour (set the default state) upon instantiation.  PHP 5 uses the __construct magic method

10 class Dog extends Animal { public $breed; function __construct($breed) { $this->breed = $breed; } function eat($food) { if($food == 'cookie') { $this->hungry = 'not so much.'; } else { echo 'barf, I only like cookies!'; } $dog = new Dog('Golden Retriever'); $dog->breed = 'Bloodhound';

11  Officially called Paamayim Nekudotayim (Hebrew for double colon, now you know what the parser is talking about when you get errors), the scope resolution operator (::) allows you to perform static calls to methods and class members.  Animal:: __construct();  parent::eat($food);

12 class Animal { public $hungry = 'hell yeah.'; function __construct() { echo 'I am an animal.'; } function eat($food) { $this->hungry = 'not so much.'; } class Dog extends Animal { public $breed; function __construct($breed) { $this->breed = $breed; Animal:: __construct(); //parent::eat($food); } function eat($food) { if($food == 'cookie') { Animal::eat($food); } else { echo 'barf, I only like cookies!'; } $dog = new Dog('Rotweiler'); $dog->eat('cookie'); echo $dog->hungry;

13  Abstract classes are, well, abstract. An abstract class isn’t intended to be instantiated, but to serve as a parent to other classes, partly dictating how they should behave. Abstract classes can have abstract methods, these are required in the child classes.

14 abstract class Animal { public $hungry = 'hell yeah.'; abstract public function eat($food); } class Dog extends Animal { function eat($food) { if($food == 'cookie') { $this->hungry = 'not so much.'; } else { echo 'barf, I only like cookies!'; } $dog = new Dog(); echo $dog->hungry; //echoes "hell yeah." $dog->eat('peanut'); //echoes "barf, I only like cookies!"

15  PHP5 features interfaces  the interface keyword creates an entity that can be used to enforce a common interface upon classes without having to extend them like with abstract classes. Instead an interface is implemented.  Interfaces are different from abstract classes. For one, they’re not actually classes. They don’t define properties, and they don’t define any behaviour. The methods declared in an interface must be declared in classes that implement it.

16 interface Animal { public function eat($food); } interface Mammal { public function whoareyou(); } class Dog implements Animal, Mammal { public $gender = 'male'; function eat($food) { if($food == 'cookie') { $this->hungry = 'not so much.'; } else { echo 'barf, I only like cookies!'; } function whoareyou() { if($this->gender == 'male') { echo ‘Yes'; } else { echo’No'; }

17  lets take an example.  Content Management System where content is a generalize form of article, reviews, blogs etc. CONTENT ARTICLE BLOGS REVIEW  So content is our base class now how we make a decision whether content class should be Abstract class, Interface or normal class.

18  if you will never make instance of base class then Abstract class and Interface are the more appropriate choice. CONTENT Publish (); ARTICLE BLOGS REVIEW  Publish having some default behavior prefer content class as an Abstract class  If there is no default behavior for the “Publish” and every drive class makes their own implementation then there is no need to implement “Publish” behavior in the base case I’ll prefer Interface.

19  The __autoload() magic method of PHP5 get automatically called whenever you try to load an object of class which resides in separate file and you have not included those files using include,require and include_once. It is recommended to use the filename as that of the class name. <? function __autoload($classname) { include $classname.".php"; //Here $classname=magicmethod1 } $a = new magicmethod1(); ?>

20  Destructors are another type of magic methods. Indicated by __destruct, these methods are called when all references to their objects are removed. This includes explicit un-setting and script shutdown. class Example { private $_name; public function __construct($name) { $this->_name = $name; } function __destruct() { echo "Destructing object '$this->_name'.". PHP_EOL; } $objectOne = new Example('Object one'); $objectTwo = new Example('Object two'); unset($objectOne); echo 'Script still running.'. PHP_EOL;

21  PHP 5 allows you to declare the visibility of methods and properties. There are three types of visibility: public, protected and private.  Public Public methods and properties are visible (accessible) to any code that queries them.  Protected Requests are only allowed from within the objects blueprint (that includes parent and child classes).  Private Access is limited to the declaring class (the class the property is declared in). No external access whatsoever is allowed.

22 PHP 5 features type hinting as a means to limit the types of variables that a method will accept as an argument. class Rabbit {} class Feeder { public function feedRabbit(Rabbit $rabbit, $food) { $rabbit->eat($food); } $dog = new Dog(); $feeder = new Feeder(); $feeder->feedRabbit($dog, 'broccoli'); Attempting to use an instance of Dog with the feedRabbit method results in the following error: Fatal error: Argument 1 passed to Feeder::feedRabbit() must be an instance of Rabbit

23  PHP 5 introduces the concept of exceptions to PHP. An exception is not an error, an uncaught exception is (a fatal error). class LiarException extends Exception {} try { if($doggy->talk() == 'Doggie likes broccoli.') { throw new LiarException( 'Doggie is a big fat liar. He only likes cookies.' ); } else { throw new Exception('Just because we can.'); } echo 'An exception was thrown, so this will never print...'; } catch(LiarException $e) { echo "Somebody lied about something: {$e->getMessage()}'"; } catch(Exception $e) { echo "Somebody threw an exception: {$e->getMessage()}'"; }

24  The final keyword is used to declare that a function or class cannot be overridden by a sub-class. This is another way of stopping other programmers using your code outside the bounds you had planned for it. class dog { private $Name; private $DogTag; final public function bark() { print "Woof!\n"; }

25  Name; } } class poodle extends dog { public function bark() { print "'Woof', says ". $this->getName(); } } ?>

26  This is the first of three slightly unusual magic functions, and allows you to specify what to do if an unknown class variable is read from within your script. Take a look at the following script:  Age; ?> Note that our dog class has $Age commented out, and we attempt to print out the Age value of $poppy. When this script is called, $poppy is found to not to have an $Age variable, so __get() is called for the dog class, which prints out the name of the property that was requested - it gets passed in as the first parameter to __get(). If you try uncommenting the public $Age; line, you will see __get() is no longer called, as it is only called when the script attempts to read a class variable that does not exist.

27  The __set() magic function complements __get(), in that it is called whenever an undefined class variable is set in your scripts. Name = $Name; } public function __set($var, $val) { mysql_query("UPDATE {$this->Name} SET $var = '$val';"); } // public $AdminEmail = 'foo@bar.com'; } $systemvars = new mytable("systemvars"); $systemvars->AdminEmail = 'telrev@somesite.net'; ?>

28  The call() magic function is to class functions what __get() is to class variables - if you call meow() on an object of class dog, PHP will fail to find the function and check whether you have defined a __call() function. If so, your __call() is used, with the name of the function you tried to call and the parameters you passed being passed in as parameters one and two respectively.  meow("foo", "bar", "baz"); ?>

29  This tutorial will guide you through the __toString() Magic Method. The __toString() Magic method in PHP5 get called while trying to print or echo the class objects. This method can be used print all the methods and attributes defined for the class at runtime for debugging. Also this method can be used to give error message while somebody tries to print the class details. < ? class magicmethod { function __toString() { return "Caught You!! Cannot access the Class Object"; } $a = new magicmethod(); echo $a; ?>

30  In PHP 5 when you assign one object to another object creates a reference copy and does not create duplicate copy. This would create a big mess as all the object will share the same memory defined for the object. To counter this PHP 5 has introduced clone method which creates an duplicate copy of the object. __clone magic method automatically get called whenever you call clone methods.  $tiger = new Animal();  $tiger->name = "Tiger";  $tiger->legs = 4;  $kangaroo = clone $tiger;


Download ppt " Stands for "Object-Oriented Programming." OOP refers to a programming methodology based on objects, instead of just functions and procedures. These objects."

Similar presentations


Ads by Google