0% found this document useful (0 votes)
43 views44 pages

Oo PHP

The document provides an overview of Object-Oriented Programming (OOP) in PHP, highlighting its focus on classes and objects as opposed to procedural programming. It covers key concepts such as constructors, destructors, inheritance, access modifiers, abstract classes, and interfaces, along with practical exercises for better understanding. The advantages of OOP include modular structure, easier maintenance, and code reusability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views44 pages

Oo PHP

The document provides an overview of Object-Oriented Programming (OOP) in PHP, highlighting its focus on classes and objects as opposed to procedural programming. It covers key concepts such as constructors, destructors, inheritance, access modifiers, abstract classes, and interfaces, along with practical exercises for better understanding. The advantages of OOP include modular structure, easier maintenance, and code reusability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

OBJECTED ORIENTED

PHP
Objected oriented php
• Object-Oriented Programming (OOP) is a 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.
Objected oriented php(contd.)
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 makes 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.
Understanding 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.

• A class acts as a template or blueprint from which lots of


individual objects can be created.

• When individual objects are created, they inherit the same


generic properties and behaviors, although each object may
have different values for certain properties.
Understanding Classes and Objects(contd.)
• The arrow symbol (->) is an OOP construct that is used to
access contained properties and methods of a given
object.

• Whereas, the pseudo-variable $this provides a reference


to the calling object i.e. the object to which the method
belongs.

• The real power of object oriented programming becomes


evident when using multiple instances of the same class,
Using Constructors and Destructors
• To make the object-oriented programming easier, PHP
provides some magic methods that are executed
automatically when certain actions occur within an object.

• For example, the magic method __construct() (known as


constructor) is executed automatically whenever a new
object is created. Similarly, the magic method __destruct()
(known as destructor) is executed automatically when the
object is destroyed. A destructor function cleans up any
resources allocated to an object once the object is
destroyed.
Using Constructors and Destructors(contd.)
• A destructor is called automatically when a scripts ends.
However, to explicitly trigger the destructor, you can destroy the
object using the PHP unset() 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.

• __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
• Exercise: Create a Student Class with Form Input

• Task: Create a class Student with the following properties:

o name
o age
o course

1. The constructor should initialize these properties based on values received


from a form.
2. The form should accept user input for the name, age, and course fields.
3. After form submission, display the student details by creating an instance
of the Student class using the submitted form data.
Extending Classes through Inheritance
• Classes can inherit the properties and methods of another
class using the extends keyword.

• This process of extensibility is called inheritance.

• Class derived extends base{

•}
Access Modifiers
• public - the property or method can be accessed from
everywhere. This is default
• protected - the property or method can be accessed
within the class and by classes derived from that class
• private - the property or method can ONLY be accessed
within the class
$test->name=“xyz”;
$test->name=“xyz”;
$test->name=“xyz”;
Scenario 1: Bank Account Management
Objective: Use private and public access modifiers to restrict and allow access
to sensitive information.
1. Class: BankAccount
o Properties:
 private $accountNumber: The bank account number should be private
to prevent unauthorized access.
 private $balance: The balance is also private to ensure only the
account holder or authorized functions can view or modify it.
o Methods:
 public function deposit($amount): Allows the user to deposit money
(public).
 public function withdraw($amount): Allows the user to withdraw
money (public).
 public function getBalance(): Allows the user to view the balance
(public).
Scenario 4: Online Store with Product Classes
Objective: Demonstrate different access levels for sharing product data.
1. Class: Product
o Properties:
 protected $name: Can be accessed by subclasses like
DigitalProduct.
 private $price: Not accessible outside of Product.
o Methods:
 public function __construct($name, $price): Initializes product
name and price.
 public function displayInfo(): Displays product name and price.
2. Subclass: DigitalProduct (extends Product)
o Properties:
 private $downloadLink: Only accessible within DigitalProduct.
o Methods:
 public function __construct($name, $price, $downloadLink):
Initializes product details with download link.
 public function displayInfo(): Displays product name, price, and
download link.
Abstract class
• Abstract classes and methods are when the parent class
has a named method, but need its child class(es) to fill out
the tasks.

• An abstract class is a class that contains at least one


abstract method.

• When inheriting from an abstract class, the child class


method must be defined with the same name, and the
access modifier.
Abstract class(contd.)
• The child class method must be defined with the same
name and it re-declares the parent abstract method.

• The child class method must be defined with the same


access modifier.

• The number of required arguments must be the same.


Abstract class(contd.)
• We can declare a class as abstract by affixing the name of the
class with the abstract keyword.

• The definition is very clear, the class that contains abstract


methods is known as abstract class.

• Abstract methods define in the abstract class just have name


and arguments, and no other code.

• An object of an abstract class can't be made. Rather, we need


to extend child classes that compute the definition of the
function into the bodies of the abstract methods in the child
classes and utilize these child classes to create objects.
Abstract class(contd.) – Imp facts
• An abstract class can have abstract along with non
abstract methods.
• Cannot be directly instantiate
• Same (or a less restricted) visibility
• An abstract method can not contain body
• Any class that contains at least one abstract method must
be declared as abstract class
$test= new A(“Harsh”);
Interface
• An Interface enables us to make programs, indicating the
public methods that a class must execute, without
including the complexities and procedure of how the
specific methods are implemented.

• This implies that an interface can define method names


and arguments, but not the contents of the methods.

• Any classes implementing an interface must implement all


methods defined by the interface.
Difference between abstract class and
interface
Abstract class
• Abstract class comes under partial abstraction.
• Abstract classes can maintain abstract methods and non
abstract methods.
• In abstract classes, we can create the variables.
• In abstract classes, we can use any access specifier.
• By using 'extends' keyword we can access the abstract
class features from derived class.
• Multiple inheritance is not possible.
Difference between abstract class and
interface(contd.)
Interface
• Interface comes under fully abstraction.
• Interfaces can maintain only abstract methods.
• In interfaces, we can't create the variables.
• In interface, we can use only public access specifier.
• By using 'implement' keyword we can get interface from
derived class.
• By using interfaces multiple inheritance is possible.
• Exercise : Animal and Dog Inheritance with Form

• Task:

1. Create a base class Animal with properties:


o name
o species

• Add a constructor to initialize these properties and a method makeSound()


to display a generic sound (e.g., "Animal makes a sound").
2. Create a derived class Dog that extends Animal and adds a property breed.
Override the makeSound() method to display "Dog barks."
3. Create an HTML form to accept inputs for name, species, and breed, and
display the dog's sound using the Dog class.
• 2. User Authentication Interface

• Implement an interface to authenticate users based on login form


submissions.
• Instructions:

1. Define an interface Authenticator with a method authenticate().

2. Create classes AdminAuthenticator and UserAuthenticator, each


implementing Authenticator to check credentials.
3. On form submission, choose the appropriate authenticator based
on user role.

You might also like