PHP is an object-oriented programming language that supports fundamental object-oriented programming features like encapsulation, inheritance, and polymorphism. Key concepts covered in the document include:
- Classes allow data and functions to be grouped together, and instances of classes are called objects.
- Access specifiers like public, private, and protected determine visibility of class members. Private members can only be accessed within the class, while public members can be accessed anywhere.
- Constructors are special methods that initialize an object when it is created. Destructors are called when objects are destroyed.
- Inheritance allows new classes to inherit attributes and behaviors from existing classes. Polymorphism allows different classes to have similarly
Object oriented programming assume everything as an object
Class − This is a programmer-defined data type, which includes local functions as well as local data.
Object − An individual instance of the data structure defined by a class.
Member Variable − These are the variables defined inside a class.
Member function − These are the function defined inside a class and are used to access object data.
Inheritance − When a class is defined by inheriting existing function of a parent class then it is called inheritance.
Parent class − A class that is inherited from by another class. This is also called a base class or super class.
Child Class − A class that inherits from another class. This is also called a subclass or derived class.
Polymorphism − This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it take different number of arguments and can do different task.
Data Abstraction − Any representation of data in which the implementation details are hidden (abstracted).
Encapsulation − refers to a concept where we encapsulate all the data and member functions together to form an object.
Constructor − refers to a special type of function which will be called automatically whenever there is an object formation from a class.
Destructor − refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.
....
Demystifying Object-Oriented Programming - Lone Star PHPAlena Holligan
Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.
Abstraction, encapsulation, polymorphism, and interfaces: whether you've been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole object-oriented thing is way more complicated. Not only that, people that already understand it, act like it's so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session, we'll take you through real-life examples of object-oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilize object-oriented programming (OOP) immediately.
Abstraction, Encapsulation, Polymorphism, Interfaces and Scope: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense.
Real life examples of Object-Oriented terminology in a way that makes sense and allows you to utilizing OOP immediately.
Object oriented programming assume everything as an object
Class − This is a programmer-defined data type, which includes local functions as well as local data.
Object − An individual instance of the data structure defined by a class.
Member Variable − These are the variables defined inside a class.
Member function − These are the function defined inside a class and are used to access object data.
Inheritance − When a class is defined by inheriting existing function of a parent class then it is called inheritance.
Parent class − A class that is inherited from by another class. This is also called a base class or super class.
Child Class − A class that inherits from another class. This is also called a subclass or derived class.
Polymorphism − This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it take different number of arguments and can do different task.
Data Abstraction − Any representation of data in which the implementation details are hidden (abstracted).
Encapsulation − refers to a concept where we encapsulate all the data and member functions together to form an object.
Constructor − refers to a special type of function which will be called automatically whenever there is an object formation from a class.
Destructor − refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.
....
Demystifying Object-Oriented Programming - Lone Star PHPAlena Holligan
Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.
Abstraction, encapsulation, polymorphism, and interfaces: whether you've been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole object-oriented thing is way more complicated. Not only that, people that already understand it, act like it's so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session, we'll take you through real-life examples of object-oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilize object-oriented programming (OOP) immediately.
Abstraction, Encapsulation, Polymorphism, Interfaces and Scope: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense.
Real life examples of Object-Oriented terminology in a way that makes sense and allows you to utilizing OOP immediately.
Demystifying Object-Oriented Programming - PHP UK Conference 2017Alena Holligan
Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.
Coming to Terms with OOP In Drupal - php[world] 2016Chris Tankersley
Drupal 8 has not only brought to the table a much improved admin experience, but has now moved on from its procedural roots into the realm of Object Oriented Programming. While this is a great thing for developers, many Drupal developers have never been introduced to OOP. This talk will explore a very high-level overview of objects, inheritance, composition, and how to architect your code as it relates to Drupal 8.
Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.
This talk is an overview of the history of the PHP language and major framework projects that have emerged in the last 5 years. It examines what we've learned in the development of these frameworks, how that education has been brought to bear in Lithium. Most of this talk ended up being me demoing and answering questions, so there's not a lot of content in the slides, sorry.
Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.
Webinar topic: Migration from Procedural to OOP (Indonesian)
Presenter: Achmad Mardiansyah
In this webinar series, We are discussing a migration of the PHP development model from Procedural to OOP (Object Oriented Programming). This slide is a part of PHP ID (PHP Indonesia) Online Learning #6.
Please share your feedback or webinar ideas here: http://bit.ly/glcfeedback
Check our schedule for future events: https://www.glcnetworks.com/schedule/
Follow our social media for updates: Facebook, Instagram, YouTube Channel, and telegram
Recording available on Youtube
https://youtu.be/y3w6H04yPDU
PHPID online Learning #6 Migration from procedural to OOPAchmad Mardiansyah
In this presentation, we will be talking about a migration of PHP development model from Procedural to OOP (Object Oriented Programming). This slide is a part of PHP ID (PHP Indonesia) Online Learning #6
Demystifying Object-Oriented Programming - PHP UK Conference 2017Alena Holligan
Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.
Coming to Terms with OOP In Drupal - php[world] 2016Chris Tankersley
Drupal 8 has not only brought to the table a much improved admin experience, but has now moved on from its procedural roots into the realm of Object Oriented Programming. While this is a great thing for developers, many Drupal developers have never been introduced to OOP. This talk will explore a very high-level overview of objects, inheritance, composition, and how to architect your code as it relates to Drupal 8.
Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.
This talk is an overview of the history of the PHP language and major framework projects that have emerged in the last 5 years. It examines what we've learned in the development of these frameworks, how that education has been brought to bear in Lithium. Most of this talk ended up being me demoing and answering questions, so there's not a lot of content in the slides, sorry.
Abstraction, Encapsulation, Polymorphism, and Interfaces: whether you’ve been programming in PHP for years or are just starting your journey, these terms can be overwhelming even on a good day. Variables, conditionals, those all make sense. But this whole Object-Oriented thing is WAY more complicated. Not only that, people that already understand it act like it’s so easy and they talk right over the simple questions and never explain the basic concepts in a way that actually makes sense. In this session we’ll take you through real life examples of Object-Oriented terminology in a way that will make sense of all the mumbo jumbo and allow you to utilizing OOP immediately.
Webinar topic: Migration from Procedural to OOP (Indonesian)
Presenter: Achmad Mardiansyah
In this webinar series, We are discussing a migration of the PHP development model from Procedural to OOP (Object Oriented Programming). This slide is a part of PHP ID (PHP Indonesia) Online Learning #6.
Please share your feedback or webinar ideas here: http://bit.ly/glcfeedback
Check our schedule for future events: https://www.glcnetworks.com/schedule/
Follow our social media for updates: Facebook, Instagram, YouTube Channel, and telegram
Recording available on Youtube
https://youtu.be/y3w6H04yPDU
PHPID online Learning #6 Migration from procedural to OOPAchmad Mardiansyah
In this presentation, we will be talking about a migration of PHP development model from Procedural to OOP (Object Oriented Programming). This slide is a part of PHP ID (PHP Indonesia) Online Learning #6
About
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Technical Specifications
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
Key Features
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface
• Compatible with MAFI CCR system
• Copatiable with IDM8000 CCR
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
Application
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Cosmetic shop management system project report.pdfKamal Acharya
Buying new cosmetic products is difficult. It can even be scary for those who have sensitive skin and are prone to skin trouble. The information needed to alleviate this problem is on the back of each product, but it's thought to interpret those ingredient lists unless you have a background in chemistry.
Instead of buying and hoping for the best, we can use data science to help us predict which products may be good fits for us. It includes various function programs to do the above mentioned tasks.
Data file handling has been effectively used in the program.
The automated cosmetic shop management system should deal with the automation of general workflow and administration process of the shop. The main processes of the system focus on customer's request where the system is able to search the most appropriate products and deliver it to the customers. It should help the employees to quickly identify the list of cosmetic product that have reached the minimum quantity and also keep a track of expired date for each cosmetic product. It should help the employees to find the rack number in which the product is placed.It is also Faster and more efficient way.
Immunizing Image Classifiers Against Localized Adversary Attacksgerogepatton
This paper addresses the vulnerability of deep learning models, particularly convolutional neural networks
(CNN)s, to adversarial attacks and presents a proactive training technique designed to counter them. We
introduce a novel volumization algorithm, which transforms 2D images into 3D volumetric representations.
When combined with 3D convolution and deep curriculum learning optimization (CLO), itsignificantly improves
the immunity of models against localized universal attacks by up to 40%. We evaluate our proposed approach
using contemporary CNN architectures and the modified Canadian Institute for Advanced Research (CIFAR-10
and CIFAR-100) and ImageNet Large Scale Visual Recognition Challenge (ILSVRC12) datasets, showcasing
accuracy improvements over previous techniques. The results indicate that the combination of the volumetric
input and curriculum learning holds significant promise for mitigating adversarial attacks without necessitating
adversary training.
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
Terzaghi's soil bearing capacity theory, developed by Karl Terzaghi, is a fundamental principle in geotechnical engineering used to determine the bearing capacity of shallow foundations. This theory provides a method to calculate the ultimate bearing capacity of soil, which is the maximum load per unit area that the soil can support without undergoing shear failure. The Calculation HTML Code included.
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
Student information management system project report ii.pdfKamal Acharya
Our project explains about the student management. This project mainly explains the various actions related to student details. This project shows some ease in adding, editing and deleting the student details. It also provides a less time consuming process for viewing, adding, editing and deleting the marks of the students.
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxR&R Consult
CFD analysis is incredibly effective at solving mysteries and improving the performance of complex systems!
Here's a great example: At a large natural gas-fired power plant, where they use waste heat to generate steam and energy, they were puzzled that their boiler wasn't producing as much steam as expected.
R&R and Tetra Engineering Group Inc. were asked to solve the issue with reduced steam production.
An inspection had shown that a significant amount of hot flue gas was bypassing the boiler tubes, where the heat was supposed to be transferred.
R&R Consult conducted a CFD analysis, which revealed that 6.3% of the flue gas was bypassing the boiler tubes without transferring heat. The analysis also showed that the flue gas was instead being directed along the sides of the boiler and between the modules that were supposed to capture the heat. This was the cause of the reduced performance.
Based on our results, Tetra Engineering installed covering plates to reduce the bypass flow. This improved the boiler's performance and increased electricity production.
It is always satisfying when we can help solve complex challenges like this. Do your systems also need a check-up or optimization? Give us a call!
Work done in cooperation with James Malloy and David Moelling from Tetra Engineering.
More examples of our work https://www.r-r-consult.dk/en/cases-en/
Explore the innovative world of trenchless pipe repair with our comprehensive guide, "The Benefits and Techniques of Trenchless Pipe Repair." This document delves into the modern methods of repairing underground pipes without the need for extensive excavation, highlighting the numerous advantages and the latest techniques used in the industry.
Learn about the cost savings, reduced environmental impact, and minimal disruption associated with trenchless technology. Discover detailed explanations of popular techniques such as pipe bursting, cured-in-place pipe (CIPP) lining, and directional drilling. Understand how these methods can be applied to various types of infrastructure, from residential plumbing to large-scale municipal systems.
Ideal for homeowners, contractors, engineers, and anyone interested in modern plumbing solutions, this guide provides valuable insights into why trenchless pipe repair is becoming the preferred choice for pipe rehabilitation. Stay informed about the latest advancements and best practices in the field.
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdffxintegritypublishin
Advancements in technology unveil a myriad of electrical and electronic breakthroughs geared towards efficiently harnessing limited resources to meet human energy demands. The optimization of hybrid solar PV panels and pumped hydro energy supply systems plays a pivotal role in utilizing natural resources effectively. This initiative not only benefits humanity but also fosters environmental sustainability. The study investigated the design optimization of these hybrid systems, focusing on understanding solar radiation patterns, identifying geographical influences on solar radiation, formulating a mathematical model for system optimization, and determining the optimal configuration of PV panels and pumped hydro storage. Through a comparative analysis approach and eight weeks of data collection, the study addressed key research questions related to solar radiation patterns and optimal system design. The findings highlighted regions with heightened solar radiation levels, showcasing substantial potential for power generation and emphasizing the system's efficiency. Optimizing system design significantly boosted power generation, promoted renewable energy utilization, and enhanced energy storage capacity. The study underscored the benefits of optimizing hybrid solar PV panels and pumped hydro energy supply systems for sustainable energy usage. Optimizing the design of solar PV panels and pumped hydro energy supply systems as examined across diverse climatic conditions in a developing country, not only enhances power generation but also improves the integration of renewable energy sources and boosts energy storage capacities, particularly beneficial for less economically prosperous regions. Additionally, the study provides valuable insights for advancing energy research in economically viable areas. Recommendations included conducting site-specific assessments, utilizing advanced modeling tools, implementing regular maintenance protocols, and enhancing communication among system components.
4. Object Orientation
PHP is an Object-Oriented programming
language
Fundamental feature of OO is the class
PHP classes support
Encapsulation
Inheritance
Polymorphism
5.
6. What is a Class?
Classes
Sophisticated ‘variable types’
Data variables (data members) and functions
(methods) are wrapped up in a class. Collectively,
data members and methods are referred to as class
members.
An instance of a class is known as an object.
//defining a new class named Robot
class Robot {
//place class members here...
}
7.
8. Instantiating a class using new
Once a class has been created, any number of
object instances of that class can be created.
$dogRobot = new Robot();
To invoke methods:
object->method()
e.g.
<?php
....
$dogRobot = new Robot();
$dogRobot ->crawlWeb();
$dogRobot -> play();
echo $dogRobot ->talk();
...
?>
9. <?php
class Person {
private $strFirstname = “Napoleon";
private $strSurname = “Reyes";
function getFirstname() {
return $this->strFirstname;
}
function getSurname() {
return $this->strSurname;
}
}
// outside the class definition
$obj = new Person; // an object of type Person
echo "<p>Firstname: " . $obj->getFirstname() . "</p>";
echo "<p>Surname: " . $obj->getSurname() . "</p>";
?>
Defining classes
Data members
Methods
Example16-1.php
10.
11. Encapsulation
Data members are normally set inaccessible from outside
the class (as well as certain types of methods) protecting
them from the rest of the script and other classes.
This protection of class members is known as
encapsulation.
e.g.
<?php
....
$dogRobot = new Robot();
$dogRobot ->crawlWeb();
$dogRobot -> play();
echo $dogRobot ->talk();
...
?>
12. Inheritance
New classes can be defined very similar to existing ones.
All we need to do is specify the differences between the
new class and the existing one.
Data members and methods which are not defined as
being private to a class are automatically accessible by
the new class.
This is known as inheritance and is an extremely powerful
and useful programming tool.
13. Polymorphism
A concept where a number of related classes all have a
method, which shares the same name.
class Fish { draw()... //draws a fish... }
class Dog { draw()... //draws a dog... }
class Bird { draw()... //draws a bird... }
We can write a generic code that can operate on any of
these classes, invoking the appropriate draw() method
based on certain conditions.
14.
15. Example: Defining classes
class ShoppingCart {
private $name; // Name of shopper
private $items; // Items in our shopping cart
public function ShoppingCart($inputname) {
$this->name = $inputname;
}
// Add $num articles of $artnr to the cart
public function addItem($artnr, $num) {
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
public function removeItem($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
} elseif ($this->items[$artnr] == $num) {
unset($this->items[$artnr]);
return true;
} else {
return false;
}
}
}
Let’s examine the syntax of defining a class next...
16.
17. Data members and Methods
class Class1 {
private $strName = “A”;
private $intNumber = 1;
function getName() {
}
function getNumber(){
}
}
We need to provide
accessor functions to
allow users of Class1
to access the private
data members:
function getName(){
return $this->strName;
}
Is this publicly accessible?
18. $this object pointer
As with so many languages, there is a special
pointer that references an instance of a class:
$this
function getName(){
return $this->strName;
}
function getName(){
return strName;
}
19. Invoking methods inside a class
class Person{
...
function setFirstname($strSurname) {
$this->strFirstname = $strSurname;
}
function setSurname($strSurname) {
$this->strSurname = $strSurname;
}
private function display() {
echo "<p>Firstname: " . $this->strFirstname . "</p>";
echo "<p>Surname: " . $this->strSurname . "</p>";
}
function setDisplayFirstnameSurname($strFirstname, $strSurname) {
$this->setFirstname($strFirstname);
$this->setSurname($strSurname);
$this->display();
}
}
$this->functionName();
Example16-4.php
22. Private Access Specifier
class MyClassName{
private $strFirstName;
}
private – limits the
visibility of the methods
and data members only
to the class that defines
them.
23. Modifying data members
Outside the class, trying to execute
the following:
$clMyObj->intNumber++;
will fail!...
We need a method to access and change its
value:
function setNumber($intNumber) {
$this->intNumber = $intNumber;
}
intNumber is private
Look at the position of the dollar sign ($) – no
longer attached to the variable name
24. Public Access Specifier
class MyClassName{
public $strFirstName;
public function getFirstName(){
}
} public – class
members can be
accessed both within
and outside the
class.
25. Protected Access Specifier
Class MyClassName{
protected $strFirstName;
protected function getFirstName(){
}
}
Inherited protected class members –
accessible inside a derived class
Visibility of protected class
members outside the class definition
– protected class members are
inaccessible.
//protected for public use, but accessible in a
derived class
28. <?php
class MyClass {
public $public = 'Public';
protected $protected = 'Protected'; //protected for public use, but accessible in a derived class
private $private = 'Private';
function printHello() {
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
// outside the class definition
$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
//...
Example: Access Specifiers
http://php.net/manual/en/language.oop5.visibility.php
29. <?php
//...
class MyClass2 extends MyClass
{
// We can redeclare the public and protected method, but not private
// protected – ‘protected’ for public use, but accessible in a derived class
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
// outside the class definition
$obj2 = new MyClass2();
echo $obj2->public; // Works
echo $obj2->private; // Undefined
echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, Undefined
?>
Example: Access Specifiers
http://php.net/manual/en/language.oop5.visibility.php
a Derived class
31. //OUTSIDE THE CLASS DEFINITION...
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
//...
<?php
class MyClass
{
// Declare a public constructor
public function __construct() { }
// Declare a public method
public function MyPublic() { }
// Declare a protected method
protected function MyProtected() { }
// Declare a private method
private function MyPrivate() { }
// This is public
function Foo()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate();
}
}
http://php.net/manual/en/language.oop5.visibility.php
Example: Method Declaration
32. class MyClass2 extends MyClass
{
// This is public
function Foo2()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate(); // Fatal Error
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Private
Example: Method Declaration
http://php.net/manual/en/language.oop5.visibility.php
<?php
class MyClass
{
// Declare a public constructor
public function __construct() { }
// Declare a public method
public function MyPublic() { }
// Declare a protected method
protected function MyProtected() { }
// Declare a private method
private function MyPrivate() { }
// This is public
function Foo()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate();
}
}
33. $myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
// Foo::testPublic
?>
Example: Method Declaration
http://php.net/manual/en/language.oop5.visibility.php
<?php
class Bar
{
public function test() {
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "Bar::testPublicn";
}
private function testPrivate() {
echo "Bar::testPrivaten";
}
}
class Foo extends Bar
{
public function testPublic() {
echo "Foo::testPublicn";
}
private function testPrivate() {
echo "Foo::testPrivaten";
}
}
34. string(5) "hello"
Accessed the private method
Accessing Private Members of
the same object type
http://php.net/manual/en/language.oop5.visibility.php
<?php
class Test
{
private $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
private function bar()
{
echo 'Accessed the private method.';
}
public function baz(Test $other)
{
// We can change the private property:
$other->foo = 'hello';
var_dump($other->foo);
// We can also call the private method:
$other->bar();
}
}
$test = new Test('test');
$test->baz(new Test('other'));
?>
Objects of the same type will have access to each others
Private and protected members even though they are
not the same instances.
35.
36. Creating objects
•Instantiate classes using new keyword
–$myCart= new ShoppingCart(“Charlie”);
Constructors
–In earlier versions of PHP (< PHP5.3.3) Same as the
name of the class. This no longer holds!
– (PHP5 only) declared as
• public function __construct(…)
Destructors
–Declared as
–public function __destruct()
37. Latest in PHP5.3.3
<?php
namespace Foo;
class Bar {
public function Bar() {
// treated as constructor in PHP 5.3.0-5.3.2
// treated as regular method in PHP 5.3.3
}
}
?>
22 Sept. 2010
38. Constructors
A constructor is a function that does
initializations when the class is instantiated
function __construct($intNumber, $strName){
$this->set_intNumber($intNumber);
$this->set_strName($strName);
$this->printInit();//use this method
}
39. Constructors
Default arguments
function __construct ($strName = “A”, $intNumber=0) {
$this->set_intNumber($int_Number);
$this->set_strName($str_Name);
}
Instantiating a class without parameters will
make use of the default values
40. Another Example: Constructors
<?php
class vehicle {
private $strDescription;
function getDescription() {
return $this->strDescription;
}
function setDescription($strDescription) {
$this->strDescription = $strDescription;
}
function __construct ($strDescription) {
$this->strDescription = $strDescription;
}
}
?>
vehicle.php
42. Destructors
Called when objects are destroyed – free up
memory
e.g.:
function __destruct () {
echo “freeing up memory, destroying this object... <br>”;
}
This sample code above simply informs us that the object is
being destroyed already.
43. Objects as variables
Can be used in arrays
Can be passed to functions
Passed as reference all the time (PHP 5)
e.g.:
function test1($objClass1){
$objClass1->set_intName(“B”);
}
No need to use & in the formal parameter
definition. It is always passed by reference.
44. Arrays and objects
<?php
function __autoload($class_name) {
require_once $class_name . '.php';
}
$objSimon = new revisedperson("Simon", "Stobart");
$objLiz = new revisedperson("Liz", "Hall");
$objIan = new revisedperson("Ian", "Walker");
$objBilly = new revisedperson("Billy", "Lee");
$objHayley = new revisedperson("Hayley", "West");
$arrPeople = array($objSimon, $objLiz, $objIan, $objBilly, $objHayley);
foreach($arrPeople as $objThePerson){
echo($objThePerson->display());
}
?>
The function display() is common to all array elements
(elements = objects in this example).
example16-9.php
45. Multiple Object Instances
<?php
$cart1 = new ShoppingCart(“Joe Bloggs”);
$cart1->addItem("10", 1);
$cart2 = new ShoppingCart(“Fred Smith”);
$cart2->addItem("0815", 3);
?>
46. Example: Polymorphism
<?php
function __autoload($class_name) {
require_once $class_name . '.php';
}
$objRectangle = new rectangle(100,50, "rectangle.gif");
$objSquare = new square(100, "square.gif");
$objTriangle = new triangle(50,100, "triangle.gif");
$objEllipse = new ellipse(50,100, "ellipse.gif");
$arrShapes = array ($objRectangle,$objSquare,$objTriangle,$objEllipse);
foreach ($arrShapes as $objShape){
$objShape->display();
$objShape->area();
}
?>
The functions area() and display() are common to all array
elements, but executes a different formula for each type of object.
Example17-5.php
47. • Recommended: Create one PHP
source file per class definition.
• This aids class reuse and script clarity.
48. Remember these PHP Constructs?
• require(….)
Includes file specified, terminates on errors
• include(…)
Includes file specified, gives warning on errors
• require_once(….)
Includes file specified only if it has not already been
included, terminates on errors
• include_once(….)
Includes file specified only if it has not already been
included, gives warning on errors
Example16-6.php
Really useful but would require you to write a long list of include() or require() statements
at the beginning of each script, one for each class. In PHP5, this is no longer
necessary. You may define an __autoload function!
49. function __autoload()
• The function is invoked automatically each time a class is
required but has not been defined.
• We can insert this function into our script:
Example16-7.php
function __autoload($class_name) {
require_once $class_name . '.php';
}
Note: Class_name = File_name
50. function __autoload()
Example16-7.php
<?php
function __autoload($class_name) {
require_once $class_name . '.php';
}
$objSimon = new person;
$objSimon->setDisplayFirstnameSurname(“Napoleon", “Reyes");
$objBike = new vehicle("Bicycle");
echo "<p>Vehicle: " . $objBike->getDescription() . "</p>";
?>
Class definition comes from
another file.
51. Exceptions
Like all good OO languages, PHP5 supports the exception
mechanism for trapping and handling “unexpected conditions”
Note: not all exceptions are necessarily errors
Exceptions not supported in PHP4
52. class MyException extends Exception {
// Redefine the exception so message isn't
optional
public function __construct($message, $code = 0) {
// some code
// make sure everything is assigned properly
parent::__construct($message, $code);
}
// custom string representation of object
public function __toString() {
return __CLASS__ . ": $this->messagen";
}
public function customFunction() {
echo "A Custom function for this type of exceptionn";
}
}
Extend the built-in PHP Exception class with your own
exceptions (as in Java)
Exceptions
<?php
Throw new MyException("Message to display");
?>
To generate an exception
53. Objects can...
Invoke another
Be embedded within another object
Support for:
Inheritance
Scope resolution ( :: operator)
Class abstraction (define a class that does not
instantiate, use “abstract class classname”)
Polymorphism (same function names with different data
/ behaviour)
'==‘ to check if two object have the same attributes and values
'===‘ to check if two objects are the same instance of the same class
54. Advanced OO in PHP
PHP5 has rich OO support (similar to the Java model)
Single inheritance (multiple inheritance not allowed)
Abstract classes and methods
Interfaces
PHP is a reflective programming language
Names of functions/classes to be invoked do not have to be hard
wired
See also documentation at www.php.net
55. Reflection-Oriented Programming
// without reflection
$Foo = new Foo();
$Foo->hello();
// with reflection
$f = new ReflectionClass("Foo");
$m = $f->getMethod("hello");
$m->invoke( $f->newInstance() );
Normally, instructions are
executed and data is
processed; however, in some
languages, programs can
also treat instructions as
data and therefore make
reflective modifications.
http://en.wikipedia.org/wiki/Reflection_(computer_science)
Program execution could be
modified at run-time.
New in PHP, not properly
documented yet!
http://nz2.php.net/manual/en/reflectionclass.newinstance.php