PDA

View Full Version : PHP 5



jlavarj
07-18-2003, 11:42 PM
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.

jlavarj
07-22-2003, 01:50 PM
I see people voting, I can't see dead people so it's not dead people voting. Anyone got an opinion?

FZ
07-24-2003, 02:41 PM
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.

torrin
07-25-2003, 08:58 AM
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.

jlavarj
07-27-2003, 12:15 AM
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:



<?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.




<?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!

jlavarj
07-27-2003, 12:17 AM
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.