My first php class{}

Discussion in 'PHP' started by mike30, Oct 29, 2009.

  1. #1
    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:

     
    mike30, Oct 29, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    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..
     
    mastermunj, Oct 29, 2009 IP
  3. mike30

    mike30 Well-Known Member

    Messages:
    887
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    140
    #3
    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 ;)
     
    mike30, Oct 29, 2009 IP
  4. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I don't see anything wrong with it or any needs for improvement! Nice work! :)
     
    CodedCaffeine, Oct 30, 2009 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    nico_swd, Oct 31, 2009 IP
  6. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #6
    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?
     
    astrazone, Oct 31, 2009 IP
  7. mike30

    mike30 Well-Known Member

    Messages:
    887
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Classes are simply a blueprint of a set of functions to make and manipulate objects. Plus you can reuse your code with classes. ;)
     
    mike30, Oct 31, 2009 IP
  8. tonythetiger

    tonythetiger Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    tonythetiger, Nov 1, 2009 IP
  9. leprakhauns

    leprakhauns Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    leprakhauns, Nov 1, 2009 IP
  10. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #10
    Gray Fox, Nov 3, 2009 IP