6
Most read
9
Most read
12
Most read
PHP -  Introduction to  Object Oriented Programming with PHP
Introduction to Object
Oriented Programming
with PHP
Object Oriented ConceptObject Oriented Concept
 Classes, which are the "blueprints" for an object and are the
actual code that defines the properties and methods.
 Objects, which are running instances of a class and contain
all the internal data and state information needed for your
application to function.
 Encapsulation, which is the capability of an object to protect
access to its internal data
 Inheritance, which is the ability to define a class of one kind
as being a sub-type of a different kind of class (much the
same way a square is a kind of rectangle).
Creating ClassCreating Class
• Let's start with a simple example. Save the following
in a file called class.lat.php:
<?php
class Demo
{
}
?>
Adding MethodAdding Method
• The Demo class isn't particularly useful if it isn't able
to do anything, so let's look at how you can create
a method.
<?php
class Demo
{
function SayHello($name)
{
echo “Hello $name !”;
}
}
?>
Adding PropertiesAdding Properties
• Adding a property to your class is as easy as adding
a method.
<?php
class Demo
{
public $name;
function SayHello()
{
echo “Hello $this->$name !”;
}
}
?>
Object InstantiationObject Instantiation
• You can instantiate an object of type Demo like
this:
<?php
require_once('class.lat.php');
$objDemo = new Demo();
$objDemo->name = “Bayu”;
$objDemo->SayHallo();
?>
Protecting Access toProtecting Access to
Member VariablesMember Variables (1)(1) There are three different levels of visibility that a member variable or method
can have :
 Public
▪ members are accessible to any and all code
 Private
▪ members are only accessible to the class itself
 Protected
▪ members are available to the class itself, and to classes that inherit
from it
Public is the default visibility level for any member variables or functions
that do not explicitly set one, but it is good practice to always explicitly state
the visibility of all the members of the class.
Public is the default visibility level for any member variables or functions
that do not explicitly set one, but it is good practice to always explicitly state
the visibility of all the members of the class.
Protecting Access toProtecting Access to
Member VariablesMember Variables (2)(2)
• Try to change access level of property named
“name” to private of previous code.
• What the possible solution of this problem?
• Make the getter and setter function...
Always use get and set functions for your properties. Changes to business logic
and data validation requirements in the future will be much easier to
implement.
Always use get and set functions for your properties. Changes to business logic
and data validation requirements in the future will be much easier to
implement.
Class ConstantsClass Constants
 It is possible to define constant values on a per-
class basis remaining the same and
unchangeable.
 Constants differ from normal variables in that you
don't use the $ symbol to declare or use them
 The value must be a constant expression, not (for
example) a variable, a property, a result of a
mathematical operation, or a function call
Class Constants (cont.)Class Constants (cont.)
<?php
class MyClass
{
    const constant = 'constant value';
    function showConstant() {
        echo  self::constant . "n";
    }
}
echo MyClass::constant . "n";
?>
<?php
class MyClass
{
    const constant = 'constant value';
    function showConstant() {
        echo  self::constant . "n";
    }
}
echo MyClass::constant . "n";
?>
Static KeywordStatic Keyword
• Declaring class properties or methods as static
makes them accessible without needing an
instantiation of the class.
• A property declared as static can not be accessed
with an instantiated class object
PHP -  Introduction to  Object Oriented Programming with PHP
ContructorContructor
• Constructor is the method that will be implemented when object has
been initiated
• Commonly, constructor is used to initialize the object
• Use function __construct to create constructor in PHP
<?php
class Demo
{
function __construct
{
}
}
?>
DestructorDestructor
• Destructor, is method that will be run when object is
ended
<?php
class Demo
{
function __destruct
{
}
}
?>
InheritanceInheritance
• There are many benefits of inheritance with PHP, the
most common is simplifying and reducing instances of
redundant code.
Inheritance (2)Inheritance (2)
class hewan
{
protected $jml_kaki;
protected $warna_kulit;
function __construct()
{
}
function berpindah()
{
echo "Saya berpindah";
}
function makan()
{
echo "Saya makan";
}
}
class hewan
{
protected $jml_kaki;
protected $warna_kulit;
function __construct()
{
}
function berpindah()
{
echo "Saya berpindah";
}
function makan()
{
echo "Saya makan";
}
}
Inherintace (3)Inherintace (3)
TugasTugas
Tugas (cont.)Tugas (cont.)
 Class product :
 name
 price
 discount
 Class CDMusic :
 artist
 Genre
 Class CDRack
 capacity
 model
Tugas (cont.)Tugas (cont.)
 CDMusic
 Menuruni name, price dan discount dari Product
 Price = price + 10%
 Ada penambahan 5% pada discount
 CDRack
 Menuruni name, price dan discount dari Product
 Price = price + 15%
 Tidak ada penambahan discount
 Buatlah code dalam PHP, serta simulasi untuk kasus
tersebut!
ThankThank You !!!You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/php-classes-in-
mumbai.html

More Related Content

PPT
Class and Objects in PHP
PDF
Php tutorial(w3schools)
PPT
Oops concepts in php
PPT
Codeigniter
PDF
Php Tutorials for Beginners
PPT
PHP - Introduction to File Handling with PHP
PPTX
PHP slides
Class and Objects in PHP
Php tutorial(w3schools)
Oops concepts in php
Codeigniter
Php Tutorials for Beginners
PHP - Introduction to File Handling with PHP
PHP slides

What's hot (20)

PDF
JavaScript - Chapter 8 - Objects
PPT
Java Servlets
PPT
Introduction to Javascript
PPTX
Java abstract class & abstract methods
PPTX
C# classes objects
PDF
Inheritance In Java
PPT
Angular 8
PPT
Java packages
PPT
Generics in java
PPT
Java Collections Framework
PPTX
This keyword in java
PPTX
PHP FUNCTIONS
PPTX
Java script errors &amp; exceptions handling
PPTX
Ajax
PDF
Java exception handling ppt
PPT
Oops in PHP
PPT
JavaScript - An Introduction
PDF
Object-oriented Programming-with C#
JavaScript - Chapter 8 - Objects
Java Servlets
Introduction to Javascript
Java abstract class & abstract methods
C# classes objects
Inheritance In Java
Angular 8
Java packages
Generics in java
Java Collections Framework
This keyword in java
PHP FUNCTIONS
Java script errors &amp; exceptions handling
Ajax
Java exception handling ppt
Oops in PHP
JavaScript - An Introduction
Object-oriented Programming-with C#
Ad

Viewers also liked (13)

PDF
Object Oriented Programming in PHP
PPT
Object Oriented Programming Concepts
PDF
OOP in PHP
PDF
A Gentle Introduction To Object Oriented Php
DOC
Java Servlets & JSP
PPTX
C vs c++
PPT
APACHE TOMCAT
PPT
PDF
Tomcat and apache httpd training
PPT
Java Tutorial
PPT
Php Presentation
PPT
Asp.net.
Object Oriented Programming in PHP
Object Oriented Programming Concepts
OOP in PHP
A Gentle Introduction To Object Oriented Php
Java Servlets & JSP
C vs c++
APACHE TOMCAT
Tomcat and apache httpd training
Java Tutorial
Php Presentation
Asp.net.
Ad

Similar to PHP - Introduction to Object Oriented Programming with PHP (20)

PPTX
Only oop
PPTX
Lecture-10_PHP-OOP.pptx
ZIP
Object Oriented PHP5
PPT
Advanced php
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PPTX
Ch8(oop)
PPT
UNIT-IV WT web technology for 1st year cs
PPT
Synapseindia object oriented programming in php
PPTX
OOP in PHP.pptx
PDF
Object Oriented Programming in PHP
PPTX
Php oop (1)
PPTX
Object oriented programming in php
PPT
Class 7 - PHP Object Oriented Programming
DOCX
Oops concept in php
PPTX
Chap4 oop class (php) part 1
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PDF
Object Oriented PHP - PART-1
PDF
Demystifying Object-Oriented Programming #phpbnl18
Only oop
Lecture-10_PHP-OOP.pptx
Object Oriented PHP5
Advanced php
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
Ch8(oop)
UNIT-IV WT web technology for 1st year cs
Synapseindia object oriented programming in php
OOP in PHP.pptx
Object Oriented Programming in PHP
Php oop (1)
Object oriented programming in php
Class 7 - PHP Object Oriented Programming
Oops concept in php
Chap4 oop class (php) part 1
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT III (8).pptx
UNIT III (8).pptx
Object Oriented PHP - PART-1
Demystifying Object-Oriented Programming #phpbnl18

More from Vibrant Technologies & Computers (20)

PPT
Buisness analyst business analysis overview ppt 5
PPT
SQL Introduction to displaying data from multiple tables
PPT
SQL- Introduction to MySQL
PPT
SQL- Introduction to SQL database
PPT
ITIL - introduction to ITIL
PPT
Salesforce - Introduction to Security & Access
PPT
Data ware housing- Introduction to olap .
PPT
Data ware housing - Introduction to data ware housing process.
PPT
Data ware housing- Introduction to data ware housing
PPT
Salesforce - classification of cloud computing
PPT
Salesforce - cloud computing fundamental
PPT
SQL- Introduction to PL/SQL
PPT
SQL- Introduction to advanced sql concepts
PPT
SQL Inteoduction to SQL manipulating of data
PPT
SQL- Introduction to SQL Set Operations
PPT
Sas - Introduction to designing the data mart
PPT
Sas - Introduction to working under change management
PPT
SAS - overview of SAS
PPT
Teradata - Architecture of Teradata
PPT
Teradata - Restoring Data
Buisness analyst business analysis overview ppt 5
SQL Introduction to displaying data from multiple tables
SQL- Introduction to MySQL
SQL- Introduction to SQL database
ITIL - introduction to ITIL
Salesforce - Introduction to Security & Access
Data ware housing- Introduction to olap .
Data ware housing - Introduction to data ware housing process.
Data ware housing- Introduction to data ware housing
Salesforce - classification of cloud computing
Salesforce - cloud computing fundamental
SQL- Introduction to PL/SQL
SQL- Introduction to advanced sql concepts
SQL Inteoduction to SQL manipulating of data
SQL- Introduction to SQL Set Operations
Sas - Introduction to designing the data mart
Sas - Introduction to working under change management
SAS - overview of SAS
Teradata - Architecture of Teradata
Teradata - Restoring Data

Recently uploaded (20)

PPTX
CRM(Customer Relationship Managmnet) Presentation
PDF
Revolutionizing recommendations a survey: a comprehensive exploration of mode...
PDF
Child-friendly e-learning for artificial intelligence education in Indonesia:...
PDF
Be ready for tomorrow’s needs with a longer-lasting, higher-performing PC
PPTX
Slides World Game (s) Great Redesign Eco Economic Epochs.pptx
PDF
TicketRoot: Event Tech Solutions Deck 2025
PDF
Technical Debt in the AI Coding Era - By Antonio Bianco
PDF
Slides World Game (s) Great Redesign Eco Economic Epochs.pdf
PDF
TrustArc Webinar - Data Minimization in Practice_ Reducing Risk, Enhancing Co...
PDF
Fitaura: AI & Machine Learning Powered Fitness Tracker
PDF
substrate PowerPoint Presentation basic one
PDF
Introduction to c language from lecture slides
PDF
Decision Optimization - From Theory to Practice
PDF
Addressing the challenges of harmonizing law and artificial intelligence tech...
PPTX
How to use fields_get method in Odoo 18
PPTX
maintenance powerrpoint for adaprive and preventive
PDF
Examining Bias in AI Generated News Content.pdf
PDF
Advancements in abstractive text summarization: a deep learning approach
PDF
Peak of Data & AI Encore: Scalable Design & Infrastructure
PDF
State of AI in Business 2025 - MIT NANDA
CRM(Customer Relationship Managmnet) Presentation
Revolutionizing recommendations a survey: a comprehensive exploration of mode...
Child-friendly e-learning for artificial intelligence education in Indonesia:...
Be ready for tomorrow’s needs with a longer-lasting, higher-performing PC
Slides World Game (s) Great Redesign Eco Economic Epochs.pptx
TicketRoot: Event Tech Solutions Deck 2025
Technical Debt in the AI Coding Era - By Antonio Bianco
Slides World Game (s) Great Redesign Eco Economic Epochs.pdf
TrustArc Webinar - Data Minimization in Practice_ Reducing Risk, Enhancing Co...
Fitaura: AI & Machine Learning Powered Fitness Tracker
substrate PowerPoint Presentation basic one
Introduction to c language from lecture slides
Decision Optimization - From Theory to Practice
Addressing the challenges of harmonizing law and artificial intelligence tech...
How to use fields_get method in Odoo 18
maintenance powerrpoint for adaprive and preventive
Examining Bias in AI Generated News Content.pdf
Advancements in abstractive text summarization: a deep learning approach
Peak of Data & AI Encore: Scalable Design & Infrastructure
State of AI in Business 2025 - MIT NANDA

PHP - Introduction to Object Oriented Programming with PHP

  • 2. Introduction to Object Oriented Programming with PHP
  • 3. Object Oriented ConceptObject Oriented Concept  Classes, which are the "blueprints" for an object and are the actual code that defines the properties and methods.  Objects, which are running instances of a class and contain all the internal data and state information needed for your application to function.  Encapsulation, which is the capability of an object to protect access to its internal data  Inheritance, which is the ability to define a class of one kind as being a sub-type of a different kind of class (much the same way a square is a kind of rectangle).
  • 4. Creating ClassCreating Class • Let's start with a simple example. Save the following in a file called class.lat.php: <?php class Demo { } ?>
  • 5. Adding MethodAdding Method • The Demo class isn't particularly useful if it isn't able to do anything, so let's look at how you can create a method. <?php class Demo { function SayHello($name) { echo “Hello $name !”; } } ?>
  • 6. Adding PropertiesAdding Properties • Adding a property to your class is as easy as adding a method. <?php class Demo { public $name; function SayHello() { echo “Hello $this->$name !”; } } ?>
  • 7. Object InstantiationObject Instantiation • You can instantiate an object of type Demo like this: <?php require_once('class.lat.php'); $objDemo = new Demo(); $objDemo->name = “Bayu”; $objDemo->SayHallo(); ?>
  • 8. Protecting Access toProtecting Access to Member VariablesMember Variables (1)(1) There are three different levels of visibility that a member variable or method can have :  Public ▪ members are accessible to any and all code  Private ▪ members are only accessible to the class itself  Protected ▪ members are available to the class itself, and to classes that inherit from it Public is the default visibility level for any member variables or functions that do not explicitly set one, but it is good practice to always explicitly state the visibility of all the members of the class. Public is the default visibility level for any member variables or functions that do not explicitly set one, but it is good practice to always explicitly state the visibility of all the members of the class.
  • 9. Protecting Access toProtecting Access to Member VariablesMember Variables (2)(2) • Try to change access level of property named “name” to private of previous code. • What the possible solution of this problem? • Make the getter and setter function... Always use get and set functions for your properties. Changes to business logic and data validation requirements in the future will be much easier to implement. Always use get and set functions for your properties. Changes to business logic and data validation requirements in the future will be much easier to implement.
  • 10. Class ConstantsClass Constants  It is possible to define constant values on a per- class basis remaining the same and unchangeable.  Constants differ from normal variables in that you don't use the $ symbol to declare or use them  The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call
  • 11. Class Constants (cont.)Class Constants (cont.) <?php class MyClass {     const constant = 'constant value';     function showConstant() {         echo  self::constant . "n";     } } echo MyClass::constant . "n"; ?> <?php class MyClass {     const constant = 'constant value';     function showConstant() {         echo  self::constant . "n";     } } echo MyClass::constant . "n"; ?>
  • 12. Static KeywordStatic Keyword • Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. • A property declared as static can not be accessed with an instantiated class object
  • 14. ContructorContructor • Constructor is the method that will be implemented when object has been initiated • Commonly, constructor is used to initialize the object • Use function __construct to create constructor in PHP <?php class Demo { function __construct { } } ?>
  • 15. DestructorDestructor • Destructor, is method that will be run when object is ended <?php class Demo { function __destruct { } } ?>
  • 16. InheritanceInheritance • There are many benefits of inheritance with PHP, the most common is simplifying and reducing instances of redundant code.
  • 17. Inheritance (2)Inheritance (2) class hewan { protected $jml_kaki; protected $warna_kulit; function __construct() { } function berpindah() { echo "Saya berpindah"; } function makan() { echo "Saya makan"; } } class hewan { protected $jml_kaki; protected $warna_kulit; function __construct() { } function berpindah() { echo "Saya berpindah"; } function makan() { echo "Saya makan"; } }
  • 20. Tugas (cont.)Tugas (cont.)  Class product :  name  price  discount  Class CDMusic :  artist  Genre  Class CDRack  capacity  model
  • 21. Tugas (cont.)Tugas (cont.)  CDMusic  Menuruni name, price dan discount dari Product  Price = price + 10%  Ada penambahan 5% pada discount  CDRack  Menuruni name, price dan discount dari Product  Price = price + 15%  Tidak ada penambahan discount  Buatlah code dalam PHP, serta simulasi untuk kasus tersebut!
  • 22. ThankThank You !!!You !!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/php-classes-in- mumbai.html