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

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Lecture 5: Interfaces.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
PHP Workshop ‹#› PHP Classes and Object Orientation.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
What is inheritance? It is the ability to create a new class from an existing class.
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
Object-Oriented PHP (Chapter 6).
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Inheritance in the Java programming language J. W. Rider.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Programming in Java CSCI-2220 Object Oriented Programming.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance ndex.html ndex.htmland “Java.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
BY:- TOPS Technologies
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Objects as a programming concept
Inheritance and Polymorphism
Java Programming Language
Java Programming, Second Edition
Java Inheritance.
Object-Oriented Programming in PHP
Object-Oriented Programming
CIS 199 Final Review.
Object-Oriented PHP (1)
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
Presentation transcript:

 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

 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

 Encapsulation  Inheritance  Polymorphism

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

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

 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

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;

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!'; }

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

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';

 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);

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;

 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.

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!"

 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.

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'; }

 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.

 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.

 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(); ?>

 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;

 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.

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

 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()}'"; }

 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"; }

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

 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.

 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 $Admin = } $systemvars = new mytable("systemvars"); $systemvars->Admin = ?>

 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"); ?>

 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; ?>

 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;