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
PPTX
PHP slides
PPT
Oops concepts in php
PDF
Php tutorial(w3schools)
PPT
PHP variables
PDF
Php Tutorials for Beginners
PPTX
Introduction to php
Class and Objects in PHP
PHP slides
Oops concepts in php
Php tutorial(w3schools)
PHP variables
Php Tutorials for Beginners
Introduction to php

What's hot (20)

PPTX
PHP FUNCTIONS
PPTX
Files in php
PPTX
Introduction to php
PDF
jQuery for beginners
PPTX
Statements and Conditions in PHP
PPTX
Lab #2: Introduction to Javascript
PPT
PHP - Introduction to PHP AJAX
PPTX
Core java complete ppt(note)
PPTX
Form Handling using PHP
PPT
MYSQL - PHP Database Connectivity
PPTX
Php.ppt
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPT
Php forms
PPT
Php with MYSQL Database
PPSX
Javascript variables and datatypes
PPTX
Nodejs functions & modules
PPS
Java Exception handling
PPTX
PHP Variables and scopes
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PPT
Java Networking
PHP FUNCTIONS
Files in php
Introduction to php
jQuery for beginners
Statements and Conditions in PHP
Lab #2: Introduction to Javascript
PHP - Introduction to PHP AJAX
Core java complete ppt(note)
Form Handling using PHP
MYSQL - PHP Database Connectivity
Php.ppt
Basic Concepts of OOPs (Object Oriented Programming in Java)
Php forms
Php with MYSQL Database
Javascript variables and datatypes
Nodejs functions & modules
Java Exception handling
PHP Variables and scopes
PHP - DataType,Variable,Constant,Operators,Array,Include and require
Java Networking
Ad

Viewers also liked (14)

PDF
Object Oriented Programming in PHP
PPT
Oops 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
Oops 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
Information-Technology-in-Human-Society.pptx
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PDF
Introduction to MCP and A2A Protocols: Enabling Agent Communication
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PDF
Human Computer Interaction Miterm Lesson
PDF
Examining Bias in AI Generated News Content.pdf
PDF
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
PPTX
How to use fields_get method in Odoo 18
PDF
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
PDF
Altius execution marketplace concept.pdf
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
giants, standing on the shoulders of - by Daniel Stenberg
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week IV
PPTX
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
PDF
Streamline Vulnerability Management From Minimal Images to SBOMs
PDF
The AI Revolution in Customer Service - 2025
PDF
substrate PowerPoint Presentation basic one
PDF
CEH Module 2 Footprinting CEH V13, concepts
Information-Technology-in-Human-Society.pptx
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
Introduction to MCP and A2A Protocols: Enabling Agent Communication
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Human Computer Interaction Miterm Lesson
Examining Bias in AI Generated News Content.pdf
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
How to use fields_get method in Odoo 18
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
Altius execution marketplace concept.pdf
Advancing precision in air quality forecasting through machine learning integ...
giants, standing on the shoulders of - by Daniel Stenberg
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
NewMind AI Weekly Chronicles – August ’25 Week IV
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
A symptom-driven medical diagnosis support model based on machine learning te...
Streamline Vulnerability Management From Minimal Images to SBOMs
The AI Revolution in Customer Service - 2025
substrate PowerPoint Presentation basic one
CEH Module 2 Footprinting CEH V13, concepts

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