Вы находитесь на странице: 1из 3

Object Oriented Programming (OOP) Implementation in PHP 5.

3 Part -2

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. 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: This is as it sounds a property defined with this keyword can be accessed anywhere in your code once the class is instantiated. This means that the property is only accessible from within the class. That would primarily mean only the methods defined within the same Private class. This is a little more flexible than private as it allows access to Protected properties within a defined class and any descendant classes classes that inherit from it. This is a little different again in that properties defined in this way act Final similarly to the public definition except that they cannot be overridden in any descendant classes. Public

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. <?php // filename: html.php.inc class html { private $tag; public $label = "this is my label:" ; function __construct ($title) { echo "<HTML>\n<HEAD>\n<TITLE>$title</TITlE>\n</HEAD>\n<BODY>\n" ; } function pageFooter() { return "\n</body>\n</html>"; } function Image($source, $id='', $class='', $title="", $style = '', $onTypesActions="") {

//Open an img tag and set it's image source $this->tag = '<img src="' . $source . '" ' ; //Set the class attribute if ($class) $this->tag .= 'class="' . $class . '" ' ; //Set the id attribute if ($id) $this->tag .= 'id="' . $id . '" ' ; //Set the title and alt attributes if ($title) $this->tag .= 'title="' . $title . '" alt="' . $title . '" ' ; if ($style) $this->tag .= 'style="' . $style .'" '; if (!Empty($onTypesActions)){ foreach($onTypesActions AS $type => $actoin) { // The following sets ON listeners and actions $this->tag .= $type . '="' . $actoin . '" ' ; } } // close the img tag $this->tag .= " />" ; return $this->tag ; } } // end html class definition ?> 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. NewPage.php <?PHP require_once("html.php.inc"); $myHTML = NEW html("Testing page build 2"); $output = $myHTML->image("images/clock.jpg"); echo "the current value of label: " . $myHTML->label ; echo "<br /><br />"; echo "the current value of tag: " . $myHTML->tag ; echo "<br /><br />"; $output .= $myHTML->pageFooter(); echo $output;

?> 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: Next time we will look more deeply into class definitions and how they can be extended with inheritance. Connect with us on Facebook, Twitter and Flickr too!

Вам также может понравиться