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

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

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 />