0% found this document useful (0 votes)
9 views

SYBBA-CA(Advance PHP)

The document provides an overview of Object-Oriented Programming (OOP) concepts in PHP, including classes, properties, methods, and the principles of encapsulation, inheritance, abstraction, and interfaces. It explains the use of constructors and destructors, as well as introspection functions that allow examination of object characteristics. Additionally, it highlights the differences between interfaces and abstract classes, emphasizing their roles in defining structures and behaviors in PHP programming.

Uploaded by

Rani Suryvanshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SYBBA-CA(Advance PHP)

The document provides an overview of Object-Oriented Programming (OOP) concepts in PHP, including classes, properties, methods, and the principles of encapsulation, inheritance, abstraction, and interfaces. It explains the use of constructors and destructors, as well as introspection functions that allow examination of object characteristics. Additionally, it highlights the differences between interfaces and abstract classes, emphasizing their roles in defining structures and behaviors in PHP programming.

Uploaded by

Rani Suryvanshi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

SYBCA(Advance PHP)

INTROSPECTION
1.Introduction to Object Oriented
Programming in PHP

 What is class ?
Objects with similar characteristics & behaviour are grouped together into a single unit is called class.
1) Class is programmer defined data type, which includes variables & functions.
2) Define class by keyword ‘class’ followed by class name.
3) Class names are case insensitive.
4) Class includes Variables/member property & functions/methods
i) Variables within class are called properties which has name & value.
ii) Functions are called methods which represent actions associated with class.
Class, Property & Methods
Class, Property & Methods

 Syntax of class : Property :-


Using access modifier, we can change visibility of properties.
<?php 1) property of class can be defined as private , public or protected.
Class class name i) Public – properties that are accessible outside class using object should be
{ declared as Public.
//properties ii) Private – accessed by methods within same class
// Methods iii) Protected – only accessed by objects class methods & class methods of
classes inheriting from class.
} // end of class Methods:-
?> i) A method is a function defined inside a class.
ii) Within method, $this variable contains a reference to object.
Objects

 An individual instance of the data structure defined by a class is called as object.


 Building a object using a class is known as instantiation.
 Object has its own individual identity. Each object contains data & code to manipulate the
data.
Declaring of an object :-
 To declare object of a given class, the ‘new’ keyword is used.
Syntax:- Accessing Properties & Methods
$object = new Class; syntax : $object = property_name;
e.g. $object = method_name();
$emp = new Employee; e.g. : $emp->show(); &
Example of Class & Objects
Static Property & Static Method

 To access methods in terms of a class rather than object, use ‘static’ keyword.
 Inside a class, you can refer to static property using the ‘self’ keyword.
Constructor & Destructor

 PHP - The __construct Function


 A constructor allows you to initialize an object's properties upon creation of the object.
 If you create a __construct() function, PHP will automatically call this function when you create an object
from a class.
 The construct function starts with two underscores (__)!
 PHP - The __destruct Function
 A destructor is called when the object is destructed or the script is stopped or exited.
 If you create a __destruct() function, PHP will automatically call this function at the end of the script.
 Notice that the destruct function starts with two underscores (__)!
 The example below has a __construct() function that is automatically called when you create an
object from a class, and a __destruct() function that is automatically called at the end of the script
Introspection

 Introspection means looking within inside.


 Introspection is particularly used when you don’t know which class/method to be executed at the design time.
 Introspection has ability / used to examine object characteristics such as its name, parent class(if any) , properties,
methods.
 We can get more details about all these different concept & PHP offers large number of functions to complete a
particular task.
 This is concept of introspection.
 Introspection provides in-built functions such as get_class(), class_exit() ,get_declared_classes(),
get_class_methods(), get_class_vars(), method_exists(), is_subclass_of()
built-in Functions(Introspection)

 get_class() : I) returns name of class . II) Need to pass object ot class.


Syntax: get_class(object name);
 class_exits() : returns True if class is exit.
Syntax: class_exits(class name);
 get_declared_classes() : returns all classes that are loaded by PHP s/w.
Syntax: get_declared_classes();
 get_class_methods() : returns list of methods that is available within specified class.
Syntax: get_class_methods(“class name”);
In – built Functions(Introspection)

 get_class_vars() :i) returns list of class properties(variables).


ii) Returns properties(variables) of default values.
iii) There is no way to discover uninitialized properties(variables).
Syntax: get_class_vars(“class name”);
 get_object_vars() : returns array of properties(variables) that are set in object.
Syntax: get_object_vars(object name);
 method_exits() : returns true if method(function) exit.
Syntax: method_exits(object name, “class name”);
In – built Functions(Introspection)

 is_subclass_of() : returns true if specified object is subclass of specified class.


Syntax: is_subclass_of(object name, “class name”);
 get_parent_class() : returns parent class name.
Syntax: get_parent_class(“class name”);
In – built Functions(Introspection)

 get_class_vars() :i) returns list of class  get_object_vars() : returns array of


properties(variables). properties(variables) that are set in object.
 Syntax: get_class(object name);  Syntax: get_class(object name);
 Output :
Inheritance

Inheritance in OOP = When a class derives from another class.

The child class will inherit all the public and protected properties
and methods from the parent class. In addition, it can have its
own properties and methods.

An inherited class is defined by using the extends keyword.

Syntax :
class derived_classname extends base_classname
{
//properties
//methods
}

The capability of a class to derive properties and methods from another


class is called inheritance.
Inheritance(Final keyword)

The final keyword can be used to prevent class


inheritance or to prevent method overriding.

e.g.
<?php
final class emp { This example shows how to
// some code prevent class overriding
}
We get this output :
// will result in error
class manager extends emp { PHP Fatal error: Class manger may not inherit from final
// some code class (emp) final1.php on line 10
}
?>
Inheritance(Final keyword)

class emp {
final public function show() {
// some code
}
}
This example shows how to
class manager extends emp {
prevent method
// will result in error (function)overriding:
public function intro() {
// some code
}
}
?>
Abstract class

 Abstract means Incomplete. Abstraction is a way of hiding information.


 We can not create object of abstract class. The properties & methods of
abstract class is used by using inheritance property.
 In abstraction, there should be at least one method that must be declared
but not defined.
 Any class that contains at least one abstract method must be declared as
abstract.
 Syntax :
abstract class classname
{
// abstract method declaration //this is con
//non-abstract method definition
}
‘Abstract’ keyword written before class.
Interfaces

What are Intefaces ?

Interfaces allow you to specify what methods a class should


implement.

Interfaces make it easy to use a variety of different classes in the


same way. When one or more classes use the same interface, it
is referred to as "polymorphism".

Interfaces are declared with the interface keyword:


Interfaces

Characteristics of an interfaces:-

 Interfaces is similar to a class except that it cannot contain


code.
 An interface can define method names & arguments,but not
contain of methods.
 All methods(fn) declared in interface must be public.
 Any classes implementing an interface must implement all
methods defined by the interface.
 A class can implement multiple interfaces.
 An interface is declared using the ‘interface’ keyword.
 Interfaces can’t maintain Non-abstract methods.
Interfaces vs. Abstract Classes

 Interface are similar to abstract classes. The difference between interfaces and abstract classes are:
 1) Interfaces cannot have properties, while abstract classes can
 2) All interface methods must be public, while abstract class methods is public or protected
 3) All methods in an interface are abstract, so they cannot be implemented in code and the
abstract keyword is not necessary
 4) Classes can implement an interface while inheriting from another class at the same time.
 5) To implement an interface, a class must use the implements keyword.
 Advantages of Interfaces:-
 1) It separates implementation & define the structure.
 2) It is used to define generic template.
Encapsulation
Encapsulation

 The variables/data of a class are hidden from any other class and can be accessed only through any
member function of that class. The data in the class is hidden from the other classes is called as Data
Hiding.
 Encapsulation refers to protection of a class’s.
 Internal data from the code outside that class & the hiding of details of implementation. Since other
classes don’t have direct access to the properties, you can change the way the method get & set the
properties.
 Advantages of Encapsulation :-
1) You can change implementation details at any time without affecting code that uses class.
2) Outside class can’t modify property values of an object built from your class without your knowledge.

You might also like