SlideShare a Scribd company logo
1 of 3
Object Oriented Programming (OOP) Implementation in PHP 5.3 Part -2<br />Last time we looked at a basic class definition that produced some HTML tags for us. In this second part we will take you further into some definitions of OOP as it stands in PHP; then we will build out our code example to prove our theories in practice.<br />The first thing to look at this time is the topic of scope in the sense of properties (defined variables) in a class. There are 4 basic scope levels, and they are:<br />PublicThis is as it sounds – a property defined with this keyword can be accessed anywhere in your code once the class is instantiated.PrivateThis means that the property is only accessible from within the class. That would primarily mean only the methods defined within the same class.ProtectedThis is a little more flexible than private as it allows access to properties within a defined class and any descendant classes – classes that inherit from it.FinalThis is a little different again in that properties defined in this way act similarly to the public definition except that they cannot be over-ridden in any descendant classes.<br />So with these definitions let’s look at how we can make our html class a little better. Here we will build out our constructor method (refer to part one for explanation) that will allow every HTML class that we instantiate to have a basic web page structure. As well, we will add a page Footer method that will allow us to close the generated web page with grace. There will be some sample properties defined here additionally to show you the differences between the scopes as described above.<br /><?php<br />// filename: html.php.inc<br />class html {<br />private $tag;<br />public $label = quot;
this is my label:quot;
 ;<br />    function __construct ($title) {<br />        echo quot;
<HTML><HEAD><TITLE>$title</TITlE></HEAD><BODY>quot;
 ;<br />    }<br />    function pageFooter() {<br />        return quot;
</body></html>quot;
;<br />    }<br />    function Image($source, $id='', $class='', $title=quot;
quot;
, $style = '', $onTypesActions=quot;
quot;
) {<br />        //Open an img tag and set it's image source        <br />        $this->tag = '<img src=quot;
' . $source . 'quot;
 ' ;                                <br />        //Set the class attribute<br />        if ($class) $this->tag .= 'class=quot;
' . $class . 'quot;
 ' ;                                <br />        //Set the id attribute<br />        if ($id) $this->tag .= 'id=quot;
' . $id . 'quot;
 ' ;<br />        //Set the title and alt attributes<br />        if ($title)     $this->tag .= 'title=quot;
' . $title . 'quot;
 alt=quot;
' . $title . 'quot;
 ' ;    <br />        if ($style)    $this->tag .= 'style=quot;
' . $style .'quot;
 ';<br />        if (!Empty($onTypesActions)){<br />            foreach($onTypesActions AS $type => $actoin) {<br />                    // The following sets ON listeners and actions    <br />                    $this->tag .= $type . '=quot;
' . $actoin . 'quot;
 ' ;<br />            }<br />        }<br />        // close the img tag <br />        $this->tag .= quot;
 />quot;
 ;<br />        return $this->tag ;                                        <br />    }<br />} <br />// end html class definition<br />?><br />Notice here, too, that we are using a variable called “$this”. That particular variable is a pre-defined entity in every class that is used to reference the class itself. It would be the equivalent to coding $html->tag except that we can’t use $html in the class definition because we can’t be sure of what variable will be used when the class is eventually instantiated.<br />NewPage.php<br /><?PHP<br />require_once(quot;
html.php.incquot;
);<br />$myHTML = NEW html(quot;
Testing page build 2quot;
);<br />$output = $myHTML->image(quot;
images/clock.jpgquot;
);<br />echo quot;
the current value of label: quot;
 . $myHTML->label ;<br />echo quot;
<br /><br />quot;
;<br />echo quot;
the current value of tag: quot;
 . $myHTML->tag ;<br />echo quot;
<br /><br />quot;
;<br />$output .= $myHTML->pageFooter();<br />echo $output;<br />?><br />Notice here that we were able to access the publicly defined property of $label, but when we tried the same approach to $tag (the private class property) we got an error message like this if you have display_errors turned on:<br />Next time we will look more deeply into class definitions and how they can be extended with inheritance.<br />Connect with us on Facebook,  Twitter and Flickr too!<br />
Object Oriented Programming (OOP) Implementation in PHP 5.3 Part -2
Object Oriented Programming (OOP) Implementation in PHP 5.3 Part -2

More Related Content

More from OSSCube

Performance Testing Session - OSSCamp 2014
Performance Testing Session -  OSSCamp 2014Performance Testing Session -  OSSCamp 2014
Performance Testing Session - OSSCamp 2014
OSSCube
 
Why Automation is Required in Software Testing - OSSCamp 2014
Why Automation is Required in Software Testing - OSSCamp 2014Why Automation is Required in Software Testing - OSSCamp 2014
Why Automation is Required in Software Testing - OSSCamp 2014
OSSCube
 

More from OSSCube (20)

Why Does Omnichannel Experience Matter to Your Customers
Why Does Omnichannel Experience Matter to Your CustomersWhy Does Omnichannel Experience Matter to Your Customers
Why Does Omnichannel Experience Matter to Your Customers
 
Using MySQL Fabric for High Availability and Scaling Out
Using MySQL Fabric for High Availability and Scaling OutUsing MySQL Fabric for High Availability and Scaling Out
Using MySQL Fabric for High Availability and Scaling Out
 
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
 
Cutting Through the Disruption
Cutting Through the DisruptionCutting Through the Disruption
Cutting Through the Disruption
 
Legacy to industry leader: a modernization case study
Legacy to industry leader: a modernization case studyLegacy to industry leader: a modernization case study
Legacy to industry leader: a modernization case study
 
Marketing and Sales together at last
Marketing and Sales together at lastMarketing and Sales together at last
Marketing and Sales together at last
 
Using pim to maximize revenue and improve customer satisfaction
Using pim to maximize revenue and improve customer satisfactionUsing pim to maximize revenue and improve customer satisfaction
Using pim to maximize revenue and improve customer satisfaction
 
Talend for the Enterprise
Talend for the EnterpriseTalend for the Enterprise
Talend for the Enterprise
 
Ahead of the Curve
Ahead of the CurveAhead of the Curve
Ahead of the Curve
 
Non functional requirements. do we really care…?
Non functional requirements. do we really care…?Non functional requirements. do we really care…?
Non functional requirements. do we really care…?
 
Learning from experience: Collaborative Journey towards CMMI
Learning from experience: Collaborative Journey towards CMMILearning from experience: Collaborative Journey towards CMMI
Learning from experience: Collaborative Journey towards CMMI
 
Exploiting JXL using Selenium
Exploiting JXL using SeleniumExploiting JXL using Selenium
Exploiting JXL using Selenium
 
Introduction to AWS
Introduction to AWSIntroduction to AWS
Introduction to AWS
 
Maria DB Galera Cluster for High Availability
Maria DB Galera Cluster for High AvailabilityMaria DB Galera Cluster for High Availability
Maria DB Galera Cluster for High Availability
 
Talend Open Studio Introduction - OSSCamp 2014
Talend Open Studio Introduction - OSSCamp 2014Talend Open Studio Introduction - OSSCamp 2014
Talend Open Studio Introduction - OSSCamp 2014
 
Performance Testing Session - OSSCamp 2014
Performance Testing Session -  OSSCamp 2014Performance Testing Session -  OSSCamp 2014
Performance Testing Session - OSSCamp 2014
 
Job Queue Presentation - OSSCamp 2014
Job Queue Presentation - OSSCamp 2014Job Queue Presentation - OSSCamp 2014
Job Queue Presentation - OSSCamp 2014
 
Introduction to Business Process Model and Notation (BPMN) - OSSCamp 2014
 Introduction to Business Process Model and Notation (BPMN) - OSSCamp 2014 Introduction to Business Process Model and Notation (BPMN) - OSSCamp 2014
Introduction to Business Process Model and Notation (BPMN) - OSSCamp 2014
 
Apigility – Lightning Fast API Development - OSSCamp 2014
Apigility – Lightning Fast API Development - OSSCamp 2014 Apigility – Lightning Fast API Development - OSSCamp 2014
Apigility – Lightning Fast API Development - OSSCamp 2014
 
Why Automation is Required in Software Testing - OSSCamp 2014
Why Automation is Required in Software Testing - OSSCamp 2014Why Automation is Required in Software Testing - OSSCamp 2014
Why Automation is Required in Software Testing - OSSCamp 2014
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

Object Oriented Programming (OOP) Implementation in PHP 5.3 Part -2

  • 1. Object Oriented Programming (OOP) Implementation in PHP 5.3 Part -2<br />Last time we looked at a basic class definition that produced some HTML tags for us. In this second part we will take you further into some definitions of OOP as it stands in PHP; then we will build out our code example to prove our theories in practice.<br />The first thing to look at this time is the topic of scope in the sense of properties (defined variables) in a class. There are 4 basic scope levels, and they are:<br />PublicThis is as it sounds – a property defined with this keyword can be accessed anywhere in your code once the class is instantiated.PrivateThis means that the property is only accessible from within the class. That would primarily mean only the methods defined within the same class.ProtectedThis is a little more flexible than private as it allows access to properties within a defined class and any descendant classes – classes that inherit from it.FinalThis is a little different again in that properties defined in this way act similarly to the public definition except that they cannot be over-ridden in any descendant classes.<br />So with these definitions let’s look at how we can make our html class a little better. Here we will build out our constructor method (refer to part one for explanation) that will allow every HTML class that we instantiate to have a basic web page structure. As well, we will add a page Footer method that will allow us to close the generated web page with grace. There will be some sample properties defined here additionally to show you the differences between the scopes as described above.<br /><?php<br />// filename: html.php.inc<br />class html {<br />private $tag;<br />public $label = quot; this is my label:quot; ;<br />    function __construct ($title) {<br />        echo quot; <HTML><HEAD><TITLE>$title</TITlE></HEAD><BODY>quot; ;<br />    }<br />    function pageFooter() {<br />        return quot; </body></html>quot; ;<br />    }<br />    function Image($source, $id='', $class='', $title=quot; quot; , $style = '', $onTypesActions=quot; quot; ) {<br />        //Open an img tag and set it's image source        <br />        $this->tag = '<img src=quot; ' . $source . 'quot; ' ;                                <br />        //Set the class attribute<br />        if ($class) $this->tag .= 'class=quot; ' . $class . 'quot; ' ;                                <br />        //Set the id attribute<br />        if ($id) $this->tag .= 'id=quot; ' . $id . 'quot; ' ;<br />        //Set the title and alt attributes<br />        if ($title)     $this->tag .= 'title=quot; ' . $title . 'quot; alt=quot; ' . $title . 'quot; ' ;    <br />        if ($style)    $this->tag .= 'style=quot; ' . $style .'quot; ';<br />        if (!Empty($onTypesActions)){<br />            foreach($onTypesActions AS $type => $actoin) {<br />                    // The following sets ON listeners and actions    <br />                    $this->tag .= $type . '=quot; ' . $actoin . 'quot; ' ;<br />            }<br />        }<br />        // close the img tag <br />        $this->tag .= quot; />quot; ;<br />        return $this->tag ;                                        <br />    }<br />} <br />// end html class definition<br />?><br />Notice here, too, that we are using a variable called “$this”. That particular variable is a pre-defined entity in every class that is used to reference the class itself. It would be the equivalent to coding $html->tag except that we can’t use $html in the class definition because we can’t be sure of what variable will be used when the class is eventually instantiated.<br />NewPage.php<br /><?PHP<br />require_once(quot; html.php.incquot; );<br />$myHTML = NEW html(quot; Testing page build 2quot; );<br />$output = $myHTML->image(quot; images/clock.jpgquot; );<br />echo quot; the current value of label: quot; . $myHTML->label ;<br />echo quot; <br /><br />quot; ;<br />echo quot; the current value of tag: quot; . $myHTML->tag ;<br />echo quot; <br /><br />quot; ;<br />$output .= $myHTML->pageFooter();<br />echo $output;<br />?><br />Notice here that we were able to access the publicly defined property of $label, but when we tried the same approach to $tag (the private class property) we got an error message like this if you have display_errors turned on:<br />Next time we will look more deeply into class definitions and how they can be extended with inheritance.<br />Connect with us on Facebook,  Twitter and Flickr too!<br />