View Poll Results: PHP - Where lies the value?

Voters
10. You may not vote on this poll
  • Serious, large scale development

    1 10.00%
  • Quick hacks and forms

    2 20.00%
  • Jack of All Trades

    7 70.00%
Results 1 to 6 of 6

Thread: PHP 5

  1. #1
    Junior Member
    Join Date
    Jul 2003
    Location
    Logan, UT
    Posts
    18

    Default PHP 5

    I want to start a semi-serious discussion about PHP here. I've been looking over the PHP 5 specifications and it's started me thinking about php 4 and specifically about the OOP support it has. First, I come from a c++ programming background which is great for breaking a program into a hierarchy of interacting objects. With PHP 4 however the only reason I see to use classes is for encapsulation. Sure you can use inheritance and polymorphism, but there is no support for private or protected members or virtual functions in php 4. I've read that in php 5 this is going to be corrected. Are there any experienced OOP coders out there that want to sound off on this? In a stateless environment like a webpage, what good is having Objects other than to tidy up the code? Sound off. If we get a good discussion going I'll post some of my php OOP code so we can take a look at it.
    ------------------------
    I have grown 2415 times smarter since then.

  2. #2
    Junior Member
    Join Date
    Jul 2003
    Location
    Logan, UT
    Posts
    18

    Default

    I see people voting, I can't see dead people so it's not dead people voting. Anyone got an opinion?
    ------------------------
    I have grown 2415 times smarter since then.

  3. #3
    Senior Member FZ's Avatar
    Join Date
    May 2003
    Location
    Johannesburg, South Africa
    Posts
    1,024

    Default

    jlavarj,

    Personally, I don't use OOP in PHP (only in my JavaScript). PHP for me is fine as is - I don't really see the benefit of spending a lot of time learning OOP for PHP as it (OOP) does not really appeal to me. I think it is safe to say that many other people also don't use OO PHP as no one has shown interest as yet (sorry!), or maybe it's just over their heads too :lol: However, if you have some interesting tutorials or example code you want to show us, I'd love to take a look at it and comment on it.

  4. #4
    Senior Member torrin's Avatar
    Join Date
    May 2003
    Location
    Vista, CA
    Posts
    534

    Default still learning

    I'm still trying to learn php. I did a script that will display previously downloaded stock data in a yahoo type of table, but that's about it. Nothing advanced.

  5. #5
    Junior Member
    Join Date
    Jul 2003
    Location
    Logan, UT
    Posts
    18

    Default

    Okay, I'll bite. Here is some code I whipped up to take care of the mundane task of implementing web forms. I took a look at just about every OOP form example out there and was pretty unsatisfied with them becaues they tried to do things backwards. Their form classes actually create forms from the bottom up. Since most of the projects I get have forms already prototyped by the marketing and web design people, I wanted something I could just plug into and go. This is my form processor class. An example of how to use it follows:

    Code:
    <?php
    
    class CFormProcessor
    {
    	var $header_page; //path to html header template
    	var $footer_page;  //path to html footer template
    	var $error_page;  //path to html page for bad input data
    	var $confirm_page; //path to html page for good input data
    	var $form_page;  //path to actual form
    	
    	var $error_message = "";
    	var $error_seperator = "<li>";
    	
    	var $verify_fields = array();
    	var $done;
    	
    	function CFormProcessor($head,$foot,$error,$confirm,$form,$submit)
    	{
    		$this->header_page = $head;
    		$this->footer_page = $foot;
    		$this->error_page = $error;
    		$this->confirm_page = $confirm;
    		$this->form_page = $form;		
    		$this->submit_name = $submit;
    	}
    	
    	function display()
    	{
    		if ($_POST[$this->submit_name])
    		{
    			$this->verifyForm();
    	
    			if ($this->error_message == "")
    			{
    				$this->processForm();
    				$this->displayConfirm();
    			}
    			else
    			{
    				$this->displayError();
    			}
    		}
    		else
    		{
    			$this->displayForm();
    		}
    	}
    	
    	function displayForm()
    	{
    		include ($this->header_page);
    		include ($this->form_page);
    		include ($this->footer_page);
    	}
    	
    	function displayConfirm()
    	{
    		include ($this->header_page);
    		include ($this->confirm_page);
    		include ($this->footer_page);
    	}
    	
    	function displayError()
    	{
    		include ($this->header_page);
    		include ($this->error_page);
    		
    		echo "<table><tr><td><ol>". $this->error_message ."</td></tr></table></ol>";
    		
    		include ($this->footer_page);
    	}
    	
    	function verifyForm()
    	{
    		
    		if (!empty($this->verify_fields))
    		{
    			foreach ($this->verify_fields as $key => $value)
    			{
    				if (strtolower($value) == 'text')
    				{
    					if (!$this->verifyText($_POST[$key]))
    					{
    						$this->addError($key);
    					}
    				}
    				
    				if (strtolower($value) == 'email')
    				{
    					if (!$this->verifyEmail($_POST[$key]))
    					{
    						$this->addError($key);
    					}
    				}
    						
    			
    			}
    		}
    	}
    	
    	function addVerifyField($name,$type)
    	{
    		$this->verify_fields[$name] = $type;
    	}
    
    	
    	function processForm()
    	{
    		
    	}
    	
    	function verifyText($data)
    	{
    		if ($data != "")
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}
    	
    	function verifyEmail($data)
    	{
    		if (eregi("^@[[:alnum:]]|\.|-)+(\.)([a-z]{2,4})$",$data))
    		{
      		return true;
    		}
    		else
    		{
    			return false;
    		}
    	}
    
    
    	function addError($error)
    	{
    		$this->error_message .= $this->error_seperator . "<font color=\"red\"> Please enter " . $error . "</font>";
    	}
    
    	function setupEmail($to,$from,$subject,$template)
    	{
    		$this->to = $to;
    		$this->from = $from;
    		$this->subject = $subject;
    		$this->template = $template;
    	}
    	
    }
    
    ?>
    Basically the usage of this class is you define the paths to all the files you need to display the form, and then use the add-verify functions to specify which input fields on the form need to be verified and what data types they should be and then the display function determines if the form has already been posted, if it has then the input gets processed. The actual function for processing the input is blank. You can take this two ways, one if you are a functional programmer you can just fill in with however you want to handle the results of your form(email, db, textfile, etc). Or if you are familiar with OOP you would recognize this as a base class (but not abstract) that can be used to derive your own, specific form processor. So an OOP programmer would put this class in an includes directory and then anytime he/she needs to use it, would derive a new class from it specific for the project.

    This code demonstrates the weakness of PHP4 as far as objects go. There is no way to make this an abstract base class using syntax (you can use some coding tricks but it doesn't look pretty).

    If anyone wants to use this code on their own, feel free. And let me know if there are any bugs. Here is an example of how to use it to run a form, put this in the file for the main form.

    Code:
    <?php
    
    require ("formprocessor.class.php");
    
    $form = new CFormProcessor("header.php","footer.php","error.html","confirm.html","form.php","submit");
    
    
    
    //setup fields to be checked for errors
    $form->addVerifyField("email","email");
    $form->addVerifyField("firstname","text");
    $form->addVerifyField("lastname","text");
    
    
    
    $form->display();
    
    
    ?>
    See how easy that is to reuse for additional forms!
    ------------------------
    I have grown 2415 times smarter since then.

  6. #6
    Junior Member
    Join Date
    Jul 2003
    Location
    Logan, UT
    Posts
    18

    Default

    Torrin,

    Way to go. Everyone's got to start sometime on a new language. Just keep with it. A next step would be to try to get the data in realtime from the webpage where the stock data is being displayed.
    ------------------------
    I have grown 2415 times smarter since then.

Similar Threads

  1. PHP 5.2 required for WordPress: Why??
    By yourinternetradio in forum Account Maintenance
    Replies: 7
    Last Post: 02-26-2007, 04:08 PM
  2. New Site Applications (Gallery, Joomla!, PHP, phpBB, UebiMiau)
    By WestHost - BErickson in forum News / Announcements
    Replies: 0
    Last Post: 12-01-2005, 04:52 PM
  3. CURL broken after PHP 5 upgrade?
    By toastk in forum PHP / MySQL
    Replies: 0
    Last Post: 02-20-2005, 09:26 AM
  4. PHP and MySQL Site Application Upgrades
    By WestHost - MStevenson in forum News / Announcements
    Replies: 0
    Last Post: 02-01-2005, 12:55 PM
  5. Replies: 0
    Last Post: 12-18-2004, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •