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