0% found this document useful (0 votes)
44 views19 pages

Unit 3 Object Oriented PHP Partial

Uploaded by

Ut Pearls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
44 views19 pages

Unit 3 Object Oriented PHP Partial

Uploaded by

Ut Pearls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 19
CACS 205: Script Language PREP. KRISHNA PD. ACHARYA CHI MULTIPLE CAMPUS) Advance Server Sitde Scripting 7 Hrs. > Object Oriented Programming in PHP > Classes and Objects, > Defining and using properties and method > Constructions and Destructi hod Overridin, > Encapsulation, > Inheritance, > Polymorphism, > Static member > Exception handling Object Oriented Programming 15 Hrs. What is Object Oriented Programming > Object-Oriented Programming (OOP) isa programming model that is based on the concept of classes and objects. As opposed to procedural programming where the focus is on writing procedures or functions that perform operations on the data, in object-oriented programming the focus is on the creations of objects which contain both data and functions together. Object-oriented programming has several advantages over conventional or procedural style of programming. The most important ones are listed below It provides a clear modular structure for the programs. It helps you adhere to the "don't repeat yourself (DRY) principle, and thus make your code much easier to maintain, modify and debug. It makes it possible to create more complicated behavior with less code and shorter development time and high degree of reusability. | Object Oriented Programming 15 Hrs. | Classes and Objects > Classes and objects are the two main aspects of object-oriented programming. A class is a self-contained, independent collection of variables and functions which work together to perform one or more specific tasks, while objects are individual instances of a class. > Aclass acts as a template or blueprint from which lots of individual objects can be created, When individual objects are ereated, they inherit the same generic properties and behaviors, although each object may have different values for certain properties. For example, think of a class as a blueprint for a house. The blueprint itself is not a house, but is a detailed plan of the house. While, an object is like an actual house built according to that blueprint. We can build several identical houses from the same blueprint, but each house may have different paints, interiors and families inside, as shown in the illustration below. a ; pp private fname; public function t public function | Object Oriented Programming 15 Hrs. getNane ) Sperson is Object of Closs of Person ee Member function|getName,setName and member variables|$name) » syntactically, variables within a class are called properties, whereas functions are calle methods, Also class names conventionally are written in PascalCase.e. each concatenated word starts w han uppercase letter (e.g. MyC Object Oriented Programming 15 Hrs. 2Php class Car private Smake; function __get ($name) return $this->Sname; function set (Sname, $value) { $this->Sname = $value; Ce = . = ef Object Oriented Programming 15 Hrs. Controlling the Visibility of Properties and Methods When working with classes, you can evenrestrictaccess to Its properties and methods using the visibly’ keywords for greater control. There are three visibility keywords (from most visible to least visible): public, protected, private, which determines how and from where properties and methods can be accessed and modified. public — A public property or method canbe accessed anywhere, from within the class and outside. Thisis the default visibility for all class members in PHF protected — A protected property or method can only be accessed from within the class itself or in child or inherited classes i.e. classes thatextenids that class. private — A private property of method is accessible only from within the class that defines it. Even child or inherited classes cannot access private properties or methods. Object Oriented Programming 15 Hrs. PHP Constructor > Constructor function of a class get invoked automatically when an object of a class is created > Constructors does not have any return type s0 constructors does not return anything. > Constructors can take any number of input values as parameter > It just ensures that an object should go through proper initialization process. Constructors are used when we want to set any property in class when an objectis created or before an object is used. 382 Object Oriented Programming 15 Hrs. PHP Destructor > Destructors are responsible for destroying objects Destructor function invokes automatically when an object is no-longer in use in the application. It automatically closes all the available resources of an object. It automatically cleans the object from the memory. Destructor function is also useful if you want to write some clean codes like closing data connection, unset some class properties, clearing memory, closing socket connections and So on. In terms of managing the memory resources it is better to destroy an object when no longer needed, Object Oriented Programming PHP Destructor Object Oriented Programming 15 Hrs. Method overloading(polymorphism) Method overloading allows the creation of several methods with the same name which differ from each other in the type of the input parameter. class sum ( public function getsun() { Sargs = fune_get_args(); 4€ (empty (arge)) eled foreach (Jarge as Sazg) | getsun (5) Object Oriented Programming 15 Hrs. Function Overriding: > Function overriding is same as other OOPs programming languages. In function overriding, both parent and child classes should have same function name with and number of arguments. It is used to replace parent method in child class. The purpose of overriding is to change the behavior of parent class method. The two methods with the same name and same parameter is called overriding. In the example code p->hello|} method refers to P class function pony where as c->hello function refer to c->hello function. To avoid such contusion we have option p::hello() call from child class Se function (hello) in first lin Object Oriented Programming 15 Hrs. Polymorphism a The word polymorphism is derive from Greek word pee greene a poly and morphism. Ploy means many and morphism is seta sa form which means a function can be use-as many form. Or one function but different signatures: is polymorphism. Object Oriented Programming 15 Hrs. Encapsulation > Encapsulation is a concept of wrapping up or binding up related data members methods in a single class known as > Data abstraction is related to hide the essential internal property of that class private Snane; return Sehis~ tT Object Oriented Programming 15 Hrs. Inheritance: Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is amechanism where we can to derive a class from another class for a hierarchy of classes that share a set of attributes and methods. In Object Oriented Programming, whena class derives its properties and methods from another class then it is called Inheritance, The class that derives properties and methods is called the child-class or the sub-class. The class from which inheritance takes place is called the super-class or the parent-class. We use the extends keyword to inherit from a parent class. Advantage of inheritance: Save time. It help to increase code reusability. Reduces the chance of errors Makes it easier to understand the inherited class Makes it easier to understand the inherited class Make programs easier to write ObijcenOneniccirrogie n= -aIa rr Single Inheritance: In Single Inheritance one class extends another class (one class only), Class B peste Object Oriented Programming 15 Hrs. Multiple Inheritance: > In Multiple Inheritance, one class extending more than one class. PHP does not support multiple inheritance directly but it can possible with use of interface. > PHP doesn’t support multiple inheritance but by using Interfaces in PHP or using Traits in PHIP instead of classes, we can implement i | Pablie tmotion q pariic function hello peblie function weria\) rp Object Oriented Programming 15 Hrs. Multilevel Inheritance: When a class is derived from a class which is also derived from anotherclass, i.e. a class having more than one parent classes, such inheritance is called Multilevel Inheritance. oe. The level of inheritance can be extended to any number of level depending upon the relation. nel i y Object Oriented Programming 15 Hrs. Static variable: it is very handy to access methods and properties in terms of a class rather than an object. This can be done with the help of static keyword. Any method declared as static is accessible without the creation of an object. Static functions are associated with the class, not an instance of the class. They are permitted to access only static methods and static variables, To add a static method to the class, static keyword is used. ate Static Members: Static variables or static tunctions are directly related with class. It can access static variables or functions directly with class name. Use ‘static’ keyword to declare static function or static variable. a

You might also like