SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Object-Oriented PHP
R. M. S. U. Gunathilake
www.eduLanka.lk
Largest Online education School in Sri Lanka
2 / 15
Object-Oriented Programming
 Object-oriented programming (OOP) refers
to the creation of reusable software objects
that can be easily incorporated into multiple
programs
 An object refers to programming code and
data that can be treated as an individual unit
or component
 Objects are often also called components
R. M. S. U. Gunathilake – www.eduLanka.lk
Object-Oriented Programming
 Data refers to information contained within
variables or other types of storage structures
 The functions associated with an object are
called methods
 The variables that are associated with an
object are called properties or attributes
 Popular object-oriented programming
languages include C++, Java, and Visual
Basic
R. M. S. U. Gunathilake – www.eduLanka.lk 3 / 15
R. M. S. U. Gunathilake – www.eduLanka.lk
Object-Oriented Programming and Classes
 The code, methods, attributes, and other
information that make up an object are
organized into classes
 An instance is an object that has been
created from an existing class
 Creating an object from an existing class is
called instantiating the object
4 / 15
Using Objects in PHP Scripts
 PHP is a Open source server side scripting
language most widely used in web
development industry
 Declare an object in PHP by using the new
operator with a class constructor
 The syntax for instantiating an object is:
$ObjectName = new ClassName();
R. M. S. U. Gunathilake – www.eduLanka.lk 5 / 15
R. M. S. U. Gunathilake – www.eduLanka.lk
Using Objects in PHP Scripts
 The identifiers for an object name:
 Must begin with a dollar sign
 Can include numbers or an underscore
 Cannot include spaces
 Are case sensitive
$Checking = new BankAccount();
 Can pass arguments to many constructor
functions
$Checking = new BankAccount(01234587, 1021, 97.58);
6 / 15
Using Objects in PHP Scripts (continued)
 After an object is instantiated, use a hyphen
and a greater-than symbol (->) to access the
methods and properties contained in the
object
 Together, these two characters are referred
to as member selection notation
 Like functions, methods can also accept
arguments
$Checking->getBalance();
$Checking->getCheckAmount($CheckNumber);
R. M. S. U. Gunathilake – www.eduLanka.lk 7 / 15
Defining Custom PHP Classes
 Data structure refers to a system for
organizing data
 The functions and variables defined in a class
are called class members
 Class variables are referred to as data
members or member variables
 Class functions are referred to as member
functions or function members
R. M. S. U. Gunathilake – www.eduLanka.lk 8 / 15
Defining Custom PHP Classes
 Classes:
 Help make complex programs easier to manage
 Hide information that users of a class do not need
to access or know about
 Make it easier to reuse code or distribute your
code to others for use in their programs
 Inherited characteristics allow you to build
new classes based on existing classes
without having to rewrite the code contained
in the existing one
R. M. S. U. Gunathilake – www.eduLanka.lk 9 / 15
R. M. S. U. Gunathilake – www.eduLanka.lk
Creating a Class Definition (continued)
 To create a class in PHP, use the class
keyword to write a class definition
 The ClassName portion of the class definition
is the name of the new class
 Class names usually begin with an uppercase
letter to distinguish them from other identifiers
 The syntax for defining a class is:
class ClassName {
data member and member function definitions
}
10 / 15
Using Access Specifiers
 Access specifiers control a client’s access
to individual data members and member
functions
 There are three levels of access specifiers in
PHP: public, private, and protected
 The public access specifier allows anyone
to call a class’s member function or to modify
a data member
R. M. S. U. Gunathilake – www.eduLanka.lk 11 / 15
Working with Member Functions
class BankAccount {
public $Balance = 958.20;
public function withdrawal($Amount) {
$this->Balance -= $Amount;
}
}
if (class_exists("BankAccount"))
$Checking = new BankAccount();
else
exit("<p>The BankAccount class is not available!</p>");
printf("<p>Your checking account balance is $%.2f.</p>",
$Checking->Balance);
$Cash = 200;
$Checking->withdrawal(200);
printf("<p>After withdrawing $%.2f, your checking account
balance is $%.2f.</p>", $Cash, $Checking->Balance);
R. M. S. U. Gunathilake – www.eduLanka.lk 12 / 15
Initializing with Constructor Functions
 The __construct() function takes
precedence over a function with the same
name as the class
 Constructor functions are commonly used in
PHP to handle database connection tasks
R. M. S. U. Gunathilake – www.eduLanka.lk 13 / 15
Initializing with Constructor Functions
 A constructor function is a special function
that is called automatically when an object
from a class is instantiated
class BankAccount {
private $AccountNumber;
private $CustomerName;
private $Balance;
function __construct() {
$this->AccountNumber = 0;
$this->Balance = 0;
$this->CustomerName = "";
}
R. M. S. U. Gunathilake – www.eduLanka.lk 14 / 15
Finally,
 OO and Classes can be used as well as PHP
 OO PHP is popular in today
 Joomla, Moodle, Mambo, Drupal, All types of
Forum software and Latest web industry have
moved into OO PHP
 Because of OO PHP, several software, MIS,
Inventory Mgt. Systems & etc.. can be
developed in secure classes
 Also desktop Apps. will move to online Apps.
R. M. S. U. Gunathilake – www.eduLanka.lk 15 / 15
THANK YOU !
R. M. S. U. Gunathilake – www.eduLanka.lk

Weitere ähnliche Inhalte

Ähnlich wie OO PHP eduLanka.lk

StudentInformationSystemAndroidbased.pptx
StudentInformationSystemAndroidbased.pptxStudentInformationSystemAndroidbased.pptx
StudentInformationSystemAndroidbased.pptxSayantanMajhi2
 
Il 09 T3 William Spreitzer
Il 09 T3 William SpreitzerIl 09 T3 William Spreitzer
Il 09 T3 William Spreitzerwspreitzer
 
Krishna_chaitanya Java Developer
Krishna_chaitanya Java DeveloperKrishna_chaitanya Java Developer
Krishna_chaitanya Java DeveloperKRISHNA CHAITANYA S
 
Case Study For Data Governance Portal
Case Study For Data Governance PortalCase Study For Data Governance Portal
Case Study For Data Governance PortalMike Taylor
 
MOOC: Python & Web as Architecture
MOOC: Python & Web as ArchitectureMOOC: Python & Web as Architecture
MOOC: Python & Web as ArchitectureRizky Ariestiyansyah
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Jalpesh Vasa
 
Tech bug webinar Blackboard Learn OpenDB
Tech bug webinar Blackboard Learn OpenDBTech bug webinar Blackboard Learn OpenDB
Tech bug webinar Blackboard Learn OpenDBScott Hurrey
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptxrani marri
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Persistant Cookies and LDAP Injection
Persistant Cookies and LDAP InjectionPersistant Cookies and LDAP Injection
Persistant Cookies and LDAP InjectionMaulikLakhani
 
1 introduction
1 introduction1 introduction
1 introductionUtkarsh De
 
Building social and RESTful frameworks
Building social and RESTful frameworksBuilding social and RESTful frameworks
Building social and RESTful frameworksbrendonschwartz
 
1Intro.PPT
1Intro.PPT1Intro.PPT
1Intro.PPTMitrBhat
 

Ähnlich wie OO PHP eduLanka.lk (20)

StudentInformationSystemAndroidbased.pptx
StudentInformationSystemAndroidbased.pptxStudentInformationSystemAndroidbased.pptx
StudentInformationSystemAndroidbased.pptx
 
Il 09 T3 William Spreitzer
Il 09 T3 William SpreitzerIl 09 T3 William Spreitzer
Il 09 T3 William Spreitzer
 
Krishna_chaitanya Java Developer
Krishna_chaitanya Java DeveloperKrishna_chaitanya Java Developer
Krishna_chaitanya Java Developer
 
Case Study For Data Governance Portal
Case Study For Data Governance PortalCase Study For Data Governance Portal
Case Study For Data Governance Portal
 
An Overview of Entity Framework
An Overview of Entity FrameworkAn Overview of Entity Framework
An Overview of Entity Framework
 
PHP & mySQL Training in Bangalore at myTectra
PHP & mySQL Training in Bangalore at myTectraPHP & mySQL Training in Bangalore at myTectra
PHP & mySQL Training in Bangalore at myTectra
 
P mysql training in bangalore
P mysql training in bangaloreP mysql training in bangalore
P mysql training in bangalore
 
MOOC: Python & Web as Architecture
MOOC: Python & Web as ArchitectureMOOC: Python & Web as Architecture
MOOC: Python & Web as Architecture
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Tech bug webinar Blackboard Learn OpenDB
Tech bug webinar Blackboard Learn OpenDBTech bug webinar Blackboard Learn OpenDB
Tech bug webinar Blackboard Learn OpenDB
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Persistant Cookies and LDAP Injection
Persistant Cookies and LDAP InjectionPersistant Cookies and LDAP Injection
Persistant Cookies and LDAP Injection
 
Tanish Srivastava
Tanish SrivastavaTanish Srivastava
Tanish Srivastava
 
1 introduction
1 introduction1 introduction
1 introduction
 
Building social and RESTful frameworks
Building social and RESTful frameworksBuilding social and RESTful frameworks
Building social and RESTful frameworks
 
1Intro.PPT
1Intro.PPT1Intro.PPT
1Intro.PPT
 

Kürzlich hochgeladen

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

OO PHP eduLanka.lk

  • 1. Object-Oriented PHP R. M. S. U. Gunathilake www.eduLanka.lk Largest Online education School in Sri Lanka
  • 2. 2 / 15 Object-Oriented Programming  Object-oriented programming (OOP) refers to the creation of reusable software objects that can be easily incorporated into multiple programs  An object refers to programming code and data that can be treated as an individual unit or component  Objects are often also called components R. M. S. U. Gunathilake – www.eduLanka.lk
  • 3. Object-Oriented Programming  Data refers to information contained within variables or other types of storage structures  The functions associated with an object are called methods  The variables that are associated with an object are called properties or attributes  Popular object-oriented programming languages include C++, Java, and Visual Basic R. M. S. U. Gunathilake – www.eduLanka.lk 3 / 15
  • 4. R. M. S. U. Gunathilake – www.eduLanka.lk Object-Oriented Programming and Classes  The code, methods, attributes, and other information that make up an object are organized into classes  An instance is an object that has been created from an existing class  Creating an object from an existing class is called instantiating the object 4 / 15
  • 5. Using Objects in PHP Scripts  PHP is a Open source server side scripting language most widely used in web development industry  Declare an object in PHP by using the new operator with a class constructor  The syntax for instantiating an object is: $ObjectName = new ClassName(); R. M. S. U. Gunathilake – www.eduLanka.lk 5 / 15
  • 6. R. M. S. U. Gunathilake – www.eduLanka.lk Using Objects in PHP Scripts  The identifiers for an object name:  Must begin with a dollar sign  Can include numbers or an underscore  Cannot include spaces  Are case sensitive $Checking = new BankAccount();  Can pass arguments to many constructor functions $Checking = new BankAccount(01234587, 1021, 97.58); 6 / 15
  • 7. Using Objects in PHP Scripts (continued)  After an object is instantiated, use a hyphen and a greater-than symbol (->) to access the methods and properties contained in the object  Together, these two characters are referred to as member selection notation  Like functions, methods can also accept arguments $Checking->getBalance(); $Checking->getCheckAmount($CheckNumber); R. M. S. U. Gunathilake – www.eduLanka.lk 7 / 15
  • 8. Defining Custom PHP Classes  Data structure refers to a system for organizing data  The functions and variables defined in a class are called class members  Class variables are referred to as data members or member variables  Class functions are referred to as member functions or function members R. M. S. U. Gunathilake – www.eduLanka.lk 8 / 15
  • 9. Defining Custom PHP Classes  Classes:  Help make complex programs easier to manage  Hide information that users of a class do not need to access or know about  Make it easier to reuse code or distribute your code to others for use in their programs  Inherited characteristics allow you to build new classes based on existing classes without having to rewrite the code contained in the existing one R. M. S. U. Gunathilake – www.eduLanka.lk 9 / 15
  • 10. R. M. S. U. Gunathilake – www.eduLanka.lk Creating a Class Definition (continued)  To create a class in PHP, use the class keyword to write a class definition  The ClassName portion of the class definition is the name of the new class  Class names usually begin with an uppercase letter to distinguish them from other identifiers  The syntax for defining a class is: class ClassName { data member and member function definitions } 10 / 15
  • 11. Using Access Specifiers  Access specifiers control a client’s access to individual data members and member functions  There are three levels of access specifiers in PHP: public, private, and protected  The public access specifier allows anyone to call a class’s member function or to modify a data member R. M. S. U. Gunathilake – www.eduLanka.lk 11 / 15
  • 12. Working with Member Functions class BankAccount { public $Balance = 958.20; public function withdrawal($Amount) { $this->Balance -= $Amount; } } if (class_exists("BankAccount")) $Checking = new BankAccount(); else exit("<p>The BankAccount class is not available!</p>"); printf("<p>Your checking account balance is $%.2f.</p>", $Checking->Balance); $Cash = 200; $Checking->withdrawal(200); printf("<p>After withdrawing $%.2f, your checking account balance is $%.2f.</p>", $Cash, $Checking->Balance); R. M. S. U. Gunathilake – www.eduLanka.lk 12 / 15
  • 13. Initializing with Constructor Functions  The __construct() function takes precedence over a function with the same name as the class  Constructor functions are commonly used in PHP to handle database connection tasks R. M. S. U. Gunathilake – www.eduLanka.lk 13 / 15
  • 14. Initializing with Constructor Functions  A constructor function is a special function that is called automatically when an object from a class is instantiated class BankAccount { private $AccountNumber; private $CustomerName; private $Balance; function __construct() { $this->AccountNumber = 0; $this->Balance = 0; $this->CustomerName = ""; } R. M. S. U. Gunathilake – www.eduLanka.lk 14 / 15
  • 15. Finally,  OO and Classes can be used as well as PHP  OO PHP is popular in today  Joomla, Moodle, Mambo, Drupal, All types of Forum software and Latest web industry have moved into OO PHP  Because of OO PHP, several software, MIS, Inventory Mgt. Systems & etc.. can be developed in secure classes  Also desktop Apps. will move to online Apps. R. M. S. U. Gunathilake – www.eduLanka.lk 15 / 15
  • 16. THANK YOU ! R. M. S. U. Gunathilake – www.eduLanka.lk