Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Object Oriented Programming (OOP) Implementation in PHP 5.3 Part -2<br />Last time we looked at a basic class definition t...
Object Oriented Programming (OOP) Implementation in PHP 5.3 Part -2
Object Oriented Programming (OOP) Implementation in PHP 5.3 Part -2
Upcoming SlideShare
Loading in …5
×

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

2,582 views

Published on

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

Published in: Technology
  • Be the first to comment

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

  1. 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 = "this is my label:" ;<br />    function __construct ($title) {<br />        echo "<HTML>n<HEAD>n<TITLE>$title</TITlE>n</HEAD>n<BODY>n" ;<br />    }<br />    function pageFooter() {<br />        return "n</body>n</html>";<br />    }<br />    function Image($source, $id='', $class='', $title="", $style = '', $onTypesActions="") {<br />        //Open an img tag and set it's image source        <br />        $this->tag = '<img src="' . $source . '" ' ;                                <br />        //Set the class attribute<br />        if ($class) $this->tag .= 'class="' . $class . '" ' ;                                <br />        //Set the id attribute<br />        if ($id) $this->tag .= 'id="' . $id . '" ' ;<br />        //Set the title and alt attributes<br />        if ($title)     $this->tag .= 'title="' . $title . '" alt="' . $title . '" ' ;    <br />        if ($style)    $this->tag .= 'style="' . $style .'" ';<br />        if (!Empty($onTypesActions)){<br />            foreach($onTypesActions AS $type => $actoin) {<br />                    // The following sets ON listeners and actions    <br />                    $this->tag .= $type . '="' . $actoin . '" ' ;<br />            }<br />        }<br />        // close the img tag <br />        $this->tag .= " />" ;<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("html.php.inc");<br />$myHTML = NEW html("Testing page build 2");<br />$output = $myHTML->image("images/clock.jpg");<br />echo "the current value of label: " . $myHTML->label ;<br />echo "<br /><br />";<br />echo "the current value of tag: " . $myHTML->tag ;<br />echo "<br /><br />";<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 />

×