LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2004, 08:55 AM   #1
patpawlowski
Member
 
Registered: Mar 2003
Location: Centreville, Virginia
Distribution: Mandrak, Red Hat
Posts: 163

Rep: Reputation: 30
OOP in PHP


We are working on redesigning our web page which uses PHP. Right now most of the pages that I have designed follow a pretty simple format which gets complicated in the details but the overall flow is pretty simple. Each page has essentially 3 sections.

The first is a PHP switch statement that performs the appropriate "action" based on the POST method. Things such as add a record, delete a record, update a record.

Second, there is more PHP which querys the database and builds the HTML placing it in variables. A simple page may be stored in only one variable: $HTML, but normally there will be more so I can move things arround easily later.

Thirdly, is the actual HTML portion where all the $HTML variables created in section 2 are usually put into a table resulting in the final layout of the page.

What I would like to discuss, which may be beyond the scope of this board, is how to approach this in an OOP way using classes, objects, properties, and methods, and such? I understand the technical side of it well enough but I just can't get my mind around how using objects will make things better and/or easier. Maybe someone could point me to some articles that might explain what makes OOP powerful.

Thanks,
 
Old 03-04-2004, 04:47 PM   #2
Chris Weimer
Member
 
Registered: Jan 2004
Location: NYC
Distribution: Fedora XFCE
Posts: 91

Rep: Reputation: Disabled
first you have to make sure you have PHP5. Everything else below it isn't really object-oriented, but 4 does have some OOP features.
 
Old 03-04-2004, 09:12 PM   #3
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Rep: Reputation: 15
Objects i find are just neat to use once you get the hang of them. Its organized so much better than just calling functions. sometimes you get lost when a function calls a function calls another and so on but with objects everything is kinda self contained. But not every project can make full use of objects. a good script i used objects with was a box object which made boxs with shadows using divs. and i had functions like setTitle setHeight setWidth setContent. Worked out really well for making UI's for backend content input.
 
Old 09-07-2005, 09:56 PM   #4
Runnerdave
LQ Newbie
 
Registered: Jul 2005
Location: Australia
Posts: 13

Rep: Reputation: 0
this is a very good question, I have always wondered the same because I have the same type of php pages that have the same behaviour of this guy's original post, and I have always wondered if there is any OO approach for such pages.

Any comment would be greatly appreciated.
 
Old 09-08-2005, 02:31 PM   #5
Lars79
Member
 
Registered: Jan 2004
Distribution: Slackware
Posts: 96

Rep: Reputation: 15
Hello,

Fist, as Chris Weimer recommended, you should upgrade to PHP 5 (PHP 5.0.5 just came out a few days ago) to make sure you have all the new features. A tool I always use when building OOP PHP projects is Smarty (http://smarty.php.net/). Smarty is a template engine and the installation is described on their web page.

The approach I use for OOP PHP projects is build upon the MVC pattern. MVC stands for Model-View-Controller. The aim is to separate the different parts of a program from each other. The model is the heart of the application. In our approach it is the set of PHP classes you have in your project. The controller represents the user's interface to the model while the view presents the data from the model. Because in a web application the controller and the view are both presented in the web browser they are put together in a single layer, the so called template files. This is what you use a template engine like Smarty for. In web application you need an additional layer you don't have in the original MVC pattern. It's called the infrastructure. The infrastructure has two purposes. First it is the URL you type in your browser. You can't use the model (the PHP classes) and the templates for that. An infrastructe file is just a regular PHP file. Maybe it's easier to understand that with a simple example:

Imagine you want to create a contact form. You would probably have a file called contact.php. That's your infrastructe file. The first thing to do here is to check for any parameters ($_GET[], $_POST[], and so on). According to the parameters you probably have to deliver some information to the model (for example the user submitted a form). You do that by creating one or more objects of your classes and call the appropriate methods. Now the model does all the business logic. In our form it will probably validate the input data, store the data in a database, and so on. At the end the model delivers a return value, for example if the data was stored correctly or not. Now you create Smarty object in the infrastructure file:

Code:
// Include the Smarty class
require_once("Smarty.class.php");
// Create Smarty object
$smarty = new Smarty();
Depending on the return value(s), the infrastructure file decides which template file to use for the output and which variables the template file needs (which data should be displayed):

Code:
// Register the variable "foo" with the value "bar"
// You can use that variable in the template file
$smarty->assign("foo", "bar");
// Decide which template to use for output
$smarty->display("contact.tpl");
The Smarty syntax is described in the Smarty manual. You can download it on their website. In most cases you will register a return value from the model as a variable at the template. Maybe that sounds a bit complicated first, but it's quite easy if you get used to it.

That kind of approach has several advantages. Developers and designers can work independently on the project. The developers work at the model and the infrastructure and the designers at the template files. Developers won't even see a single line of HTML during the complete project while Designer won't see any PHP line. Developers can't destroy the layout and designers can't destroy the application. Due to that separation it's much easier to reuse components (classes) in other projects. You don't need to get rid of all the HTML crap before. It's also very easy to extend the project. Maybe you want to develope a WAP version of your application. All you have to do is to create a new set of template files. In the infrastructure files, depending on the source of the request, you have to decide which template (Web of WAP) to use and that's it. And of course, the maintenance of the whole application is also easier, because you don't have all the stuff in one file (HTML, CSS, JavaScript, PHP, and so on).

Unfortunately it's not possible to describe the MVC pattern and the MVC version for web projects in detail here, but maybe that gives you an idea where to start.

Good luck =),
Lars

Last edited by Lars79; 09-08-2005 at 03:11 PM.
 
Old 11-20-2005, 04:37 PM   #6
patpawlowski
Member
 
Registered: Mar 2003
Location: Centreville, Virginia
Distribution: Mandrak, Red Hat
Posts: 163

Original Poster
Rep: Reputation: 30
Excellent post! At least for me it certainly gives me a starting point which is what I was looking for.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP Help Please InvisibleSniper Programming 41 09-18-2005 03:19 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
What is true OOP? tumana Programming 4 09-13-2004 06:51 AM
When to use OOP KptnKrill Programming 10 08-24-2003 01:15 PM
newbie oop Isitme Programming 3 10-10-2001 12:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration