Hi, I am not expert on php, and I am trying to get into the OOP. This is my first class I had ever made. I tried to comment it as much as I could. Please any comment would be appreciated. To see a Demo here >>> http://mike30.me/class.image.php Here we go... <?php /*======================================================================*\ || #################################################################### || || # Wihee 1.0.0 # || || # File Version 1.0.0 # || || # File Name: class.image.php # || || # Date created: October 29, 2009 # || || # Date last updated: October 29, 2009 # || || # ---------------------------------------------------------------- # || || # Author: Miguel Fernandez. (Mike30) # || || # This file may be redistributed in whole or significant part. # || || # ------------------------- WIHEE SOFTWARE ----------------------- # || || # http://www.wihee.com | http://www.wihee.net # || || #################################################################### || \*======================================================================*/ ////////////////////////////////////////////////////////////////////////// /* // This script returns an image box within a link // here are the parameters used: @param image src @param link @param image alt @param width @param height @param image name (optional) default '' @param image id (optional) default '' @param link rel (optional) default '' */ ////////////////////////////////////////////////////////////////////////// /*======================================================================*\ || ##################################################################### || || # # || || # Edit this part with your own values. # || || # The variables are very self explanatory. # || || # # || || ##################################################################### || \*======================================================================*/ $image_src = 'http://mike30.me/images/me.png'; // your image/banner url $image_link= 'http://mike30.me'; // here goes the link $image_alt = 'Hello! This is me, Mike'; // a short description of the image "important for search engines" $image_width = '100'; // width of the image $image_height = '100'; // height of the image $image_name = 'mike'; // image name $image_id = 'mike'; // image id $link_rel = 'dofollow'; // nofollow / dofollow /*======================================================================*\ || ##################################################################### || || # # || || # End of editing. # || || # # || || ##################################################################### || \*======================================================================*/ ########################################################################### # <|------- DON'T NEED TO EDIT FORM THIS POINT BELLOW ---------|> # ########################################################################### // The image class class image{ // properties var $image_src; var $image_link; var $image_alt; var $image_width; var $image_height; var $image_name; var $image_id; var $link_rel; ////////////////////////////////////////////////////////////////////////// // method function image( $image_src, $image_link, $image_alt, $image_width, $image_height, $image_name='', $image_id='', $link_rel ='') { $this->image_src = $image_src; $this->image_link = $image_link; $this->image_alt = $image_alt; $this->image_width = $image_width; $this->image_height = $image_height; $this->image_name = $image_name; $this->image_id = $image_id; $this->link_rel = $link_rel; return $this->display='<a rel="'.$link_rel.'" href="'.$image_link.'"> <img src="'.$image_src.'" alt="'.$image_alt.'" width="'.$image_width.'" height="'.$image_height.'" name="'.$image_name.'" id="'.$image_id.'"></a>'; }// end of method }// end of image class ////////////////////////////////////////////////////////////////////////// // hold on... we are making an object... shhh... :) $new_image = new image( $image_src, $image_link, $image_alt, $image_width, $image_height, $image_name, $image_id, $link_rel ); // it was a hard work, but the object is Done! ;) ////////////////////////////////////////////////////////////////////////// // // // If you have any comment, suggestion or need any help about // // this script, post it on the forum. // // ~Mike30 :) // // // ////////////////////////////////////////////////////////////////////////// // Display the image. // If you want to include the output image in many of your pages, just // copy and paste this little code where ever you want it to be displayed. but // don't forget to include the this script in the top of your pages. // then comment the line bellow, witch is to show you how it works ?> <?php echo $new_image->display; ?> PHP:
seems you have taken more efforts in commenting the code after writing it good one at first time. hope to see a good PHP - OOP developer around soon as many people use PHP as simple scripting neglecting its OOP power..
Thanks Since I am new, I like to comment very well the code, so it's easier to understand. And at the same time others can read my code easily as well
^ I do. Your code is written in PHP 4 (which has been officially dead for quite some time). Take a look at: www.php.net/__construct http://www.php.net/language.oop5.visibility And www.php.net/language.oop5 in general. And personally, I'd make "display" a function. public function display() { echo $this->display; } PHP: $image = new image('.......'); $image->display(); PHP:
I have a little question, what is better a function or a class? I think that the same could be done with a simple function. should I start using classes instead of functions?
Classes are simply a blueprint of a set of functions to make and manipulate objects. Plus you can reuse your code with classes.
First, please take a look at PHPDocumentor, its the standard way of documenting and commenting your code. Im glad you took the initiative to comment though. PHPDoc commenting is industry standard though and allows you to create a model of your code base by running the documentor software. Second, classes vs functions is a debate and depends what your application is trying to accomplish. If you want speed, speed, speed then functional procedural programming is the route you want to take. If you want scaling, effeciency, and structure, object oriented is the way you want to go. Though for most applications not enough speed is sacrificed by going the oop route, so it makes sense to go with classes, but functions server their purpose, just make sure you realize the purpose, figure out what you want to do, and dont abuse it.
It depends on what your doing. A lot of functions and you can get more and more speghetti code and that isn't good. A good programmer will know when to use a function and when things should be an object. To get their look at real world examples of physical objects.