Cleaning up my messy code

This is a discussion on Cleaning up my messy code within the PHP General forums, part of the PHP Programming Forums category; I am working on a fairly large scale (for myself anyway) project using PHP and MySQL. I am victim of ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-29-2003
Chris
 
Posts: n/a
Default Cleaning up my messy code

I am working on a fairly large scale (for myself anyway) project using PHP
and MySQL. I am victim of teaching myself to program, not separating
presentation from my code-- all the things that lead to masses of spaghetti
code so atrocious even I can't figure out what I was doing an hour ago.

I'm not looking for an IDE or code generator so much as some practical
advice for organization and framework when developing a larger app. I know
of PHP Fusebox, having programmed with Cold Fusion fusebox for a while, but
it seems like that might be too much. Maybe I just need a sensical,
practical approach to application layout. What do you all do? How can I
learn to be a better PHP programmer in this regard?

c
Reply With Quote
  #2 (permalink)  
Old 09-29-2003
Burhan Khalid
 
Posts: n/a
Default Re: [PHP] Cleaning up my messy code

Chris wrote:

> I am working on a fairly large scale (for myself anyway) project using PHP
> and MySQL. I am victim of teaching myself to program, not separating
> presentation from my code-- all the things that lead to masses of spaghetti
> code so atrocious even I can't figure out what I was doing an hour ago.
>
> I'm not looking for an IDE or code generator so much as some practical
> advice for organization and framework when developing a larger app. I know
> of PHP Fusebox, having programmed with Cold Fusion fusebox for a while, but
> it seems like that might be too much. Maybe I just need a sensical,
> practical approach to application layout. What do you all do? How can I
> learn to be a better PHP programmer in this regard?


Chris, I'm glad that you have realized that your programming practices
are leading your into trouble. That's a big step. A lot of people refuse
to believe that.

Getting rid of spaghetti code can easily be done if you start using a
template engine ( smarty from smarty.php.net is my favorite ). This
allows you to atleast on some level, separate your presentation from
your logic.

The old mantra about documentation also holds true here. If you document
your code as you write it, it will help you try to figure out what you
were doing an hour ago.

Compartmentalizing your code and separating it into logical components
(not necessarily classes, but those help) will allow you to organize
your code better. Arranging these components into separate files will
lead to better managability of code.

As far as frameworks are concerned, I don't use any "formal" or
"published" framework, but rather have my own application skeleton that
is comprised of some PEAR classes along with a sprinkling of my own
modules. It has served me well, especially since all of the grunt work
is taken care of by PEAR.

Good luck, and I hope this helps.
--
Burhan Khalid
phplist[at]meidomus[dot]com
http://www.meidomus.com
Reply With Quote
  #3 (permalink)  
Old 09-29-2003
Brent Baisley
 
Posts: n/a
Default Re: [PHP] Cleaning up my messy code

You should search the web for articles on MVC, Model View Controller.
This is a standard technique for separating your presentation from your
logic from your database. The concept can be applied to any programming
or scripting language since it actually falls under the much bigger
realm of design patterns. I would highly recommend reading at least one
or two articles on MVC, you don't have to completely understand it, but
it will give you a could foundation.
After that, there were a few articles written on using MVC specific to
PHP over at phparch.com. I subscribed to that magazine a few months ago
and I think it's well worth it. You may need to subscribe yourself to
see the past articles, but it's not expensive for the PDF subscription
version.

I looked into the templating systems at first, but really wanted to try
what they were doing myself. It ends up being very easy to implement
using output buffering. Just look into the ob_start() command. Here is
an example of how I load a template file into a variable:
ob_start();
include("templateFile.tpl");
$tpl_content = ob_get_contents();
ob_end_clean();


On Monday, September 29, 2003, at 03:22 PM, Chris wrote:

> I am working on a fairly large scale (for myself anyway) project using
> PHP
> and MySQL. I am victim of teaching myself to program, not separating
> presentation from my code-- all the things that lead to masses of
> spaghetti
> code so atrocious even I can't figure out what I was doing an hour ago.
>
> I'm not looking for an IDE or code generator so much as some practical
> advice for organization and framework when developing a larger app. I
> know
> of PHP Fusebox, having programmed with Cold Fusion fusebox for a
> while, but
> it seems like that might be too much. Maybe I just need a sensical,
> practical approach to application layout. What do you all do? How can I
> learn to be a better PHP programmer in this regard?
>

--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
Reply With Quote
  #4 (permalink)  
Old 09-29-2003
Cpt John W. Holmes
 
Posts: n/a
Default Re: [PHP] Cleaning up my messy code

From: "Brent Baisley" <brent@landover.com>

> I looked into the templating systems at first, but really wanted to try
> what they were doing myself. It ends up being very easy to implement
> using output buffering. Just look into the ob_start() command. Here is
> an example of how I load a template file into a variable:
> ob_start();
> include("templateFile.tpl");
> $tpl_content = ob_get_contents();
> ob_end_clean();


The only "problem" with a method like that is any PHP code in your template
file is evaluated. If you're the only one writing template files, you can be
careful, but any malicious PHP code that worked it's way into the template
could cause trouble.

You could just use file_get_contents().

---John Holmes...
Reply With Quote
  #5 (permalink)  
Old 09-29-2003
Andu
 
Posts: n/a
Default Re: [PHP] Cleaning up my messy code

On 29 Sep 2003 19:22:54 -0000
Chris <chrisl_ak@hotmail.com> wrote:

> I am working on a fairly large scale (for myself anyway) project using PHP
> and MySQL. I am victim of teaching myself to program, not separating
> presentation from my code-- all the things that lead to masses of spaghetti
> code so atrocious even I can't figure out what I was doing an hour ago.
>
> I'm not looking for an IDE or code generator so much as some practical
> advice for organization and framework when developing a larger app. I know
> of PHP Fusebox, having programmed with Cold Fusion fusebox for a while, but
> it seems like that might be too much. Maybe I just need a sensical,
> practical approach to application layout. What do you all do? How can I
> learn to be a better PHP programmer in this regard?


So far I haven't heard of any school teaching php or a particularly recommended
methodology so we are all more or less in the same boat. I used the book by Luke
Wellington and Laura Thomson which aside from being a well written book uses the
same programming approach for all examples. That is: keep all html in one file and
access it as functions, split php code in logical components (build dynamic
content, database access, etc.). Not very different from when I was using cgis.
I looked at Smarty but having to learn almost a new language turned me off.
So far I tried to stay away from OO which IMHO can add unnecessary complexity if
used where functions would do just fine.
If I were to advice someone on the most important thing to look for when choosing
a method of programming that would be: don't trust anybody, don't use something
just because a lot of people use it. Evaluate and judge for yourself.

>
> c
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>



____
Regards, Andu Novac
Reply With Quote
  #6 (permalink)  
Old 09-29-2003
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Cleaning up my messy code

On Mon, 2003-09-29 at 15:22, Chris wrote:
> I am working on a fairly large scale (for myself anyway) project using PHP
> and MySQL. I am victim of teaching myself to program, not separating
> presentation from my code-- all the things that lead to masses of spaghetti
> code so atrocious even I can't figure out what I was doing an hour ago.
>
> I'm not looking for an IDE or code generator so much as some practical
> advice for organization and framework when developing a larger app. I know
> of PHP Fusebox, having programmed with Cold Fusion fusebox for a while, but
> it seems like that might be too much. Maybe I just need a sensical,
> practical approach to application layout. What do you all do? How can I
> learn to be a better PHP programmer in this regard?


I use InterJinn (yes I know I wrote it :). Personally I never liked how
smarty did things, still smells like code embedded in the templates.
InterJinn uses a tag based templating system, which is extremely easy to
add your own custom tags to easily make use of complex functionality.
unlike Smarty InterJinn doesn't load the templating system when you load
a page, since it compiles the templates and content files directly to
the page you will request. Only a tiny overhead occurs on each page load
to check file timestamps for newer versions. If a newer dependency
exists, then, and only then is the templating system loaded to updated
the requested page.

Some people will tell you not to use a non-PHP include() /require()
templating. My best guess is that they're uneducated *start the flames*.
In the case of InterJinn PHP native includes can't beat it, since it
does the include at compilations, such that at run time there is no
include to be done. Tags are easier to read than oodles of PHP code
since the tag name is often sufficient to state its purpose. Contrast:

include_once( 'header.php' )

versus:

<myProject:header/>

Or better yet for a multilingual site (probably nto even on your mind):

if( $lang == 'fr' )
{
include_once( 'header.fr.php' );
}
else
{
include_once( 'header.en.php' );
}

versus:

<myProject:header/>

InterJinn also comes with modular designed core services for managing
things like session data, caches, database, properties, forms, etc.
Databases are used via names you provide. For instance you configure all
your sites database connections in one place similar ot the following:

$GLOBALS['interJinn']['database'] = array
(
'myProject' => array
(
'host' => 'some.host.com',
'login' => 'mememe',
'password' => 'YES'
'database' => 'myProject',
),
'myRelatedProject' => array
(
'host' => 'some.host.com',
'login' => 'mememe',
'password' => 'YES'
'database' => 'oldProject',
),
'default' => 'myProject'
);

Then you can access your database in your components as follows:

$dbManager = $this->getService( 'dbManager' );

$db = $dbManager->do->getConnection( 'myProject' );
$db->do->query( 'SELECT data1, data2, FROM someTable' );

Then if you change your database settings changing the settings requires
changing one location. Also since core aspects of the framework can be
replaced with your own cooked versions, you can add to the database API
to log queries, or anything else, and without ever touching the official
distribution code. Just extend the class, then register the new Core
API. Then when you upgrade you don't need to worry about any changes
you've made. Anyways there are many, many reasons to use a framework,
and I'm obviously biased with respect to my own, but even if you don't
choose InterJinn I think you will gain a lot from the use of any of the
myriad of available templating and application frameworks that are
available.

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Reply With Quote
  #7 (permalink)  
Old 09-30-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] Cleaning up my messy code

* Thus wrote CPT John W. Holmes (holmes072000@charter.net):
> From: "Brent Baisley" <brent@landover.com>
>
> > I looked into the templating systems at first, but really wanted to try
> > what they were doing myself. It ends up being very easy to implement
> > using output buffering. Just look into the ob_start() command. Here is
> > an example of how I load a template file into a variable:
> > ob_start();
> > include("templateFile.tpl");
> > $tpl_content = ob_get_contents();
> > ob_end_clean();

>
> The only "problem" with a method like that is any PHP code in your template
> file is evaluated. If you're the only one writing template files, you can be
> careful, but any malicious PHP code that worked it's way into the template
> could cause trouble.


I wonder if now is a place to introduce my new templating system.

I have the same difficulties in letting just joe blow executing php
code within templates, sometimes smarty is just to smart for
certain applications.

Curt
--
"I used to think I was indecisive, but now I'm not so sure."
Reply With Quote
  #8 (permalink)  
Old 09-30-2003
Olinux
 
Posts: n/a
Default Re: [PHP] Cleaning up my messy code

Try out smarty (its quite simple). You will get some
ideas on how to structure. I learned that its *ok* to
have some *display logic* in your templates (i.e.
repeating rows). (yes, some will adamantly disagree)

Another thing i learned when using smarty is I like
using arrays to keep things organized.

i.e.
$article['id'], $article['title'],
$article['publish_date'], $article['author'],
$article['content']
rather than
$id, $title, $publish_date, $author, $content

olinux


--- Chris <chrisl_ak@hotmail.com> wrote:
> I am working on a fairly large scale (for myself
> anyway) project using PHP
> and MySQL. I am victim of teaching myself to
> program, not separating
> presentation from my code-- all the things that lead
> to masses of spaghetti
> code so atrocious even I can't figure out what I was
> doing an hour ago.
>
> I'm not looking for an IDE or code generator so much
> as some practical
> advice for organization and framework when
> developing a larger app. I know
> of PHP Fusebox, having programmed with Cold Fusion
> fusebox for a while, but
> it seems like that might be too much. Maybe I just
> need a sensical,
> practical approach to application layout. What do
> you all do? How can I
> learn to be a better PHP programmer in this regard?
>
> c


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com
Reply With Quote
  #9 (permalink)  
Old 09-30-2003
Cristian Lavaque
 
Posts: n/a
Default Re: [PHP] Cleaning up my messy code

olinux wrote:
> Try out smarty (its quite simple). You will get some
> ideas on how to structure. I learned that its *ok* to
> have some *display logic* in your templates (i.e.
> repeating rows). (yes, some will adamantly disagree)
>
> Another thing i learned when using smarty is I like
> using arrays to keep things organized.
>
> i.e.
> $article['id'], $article['title'],
> $article['publish_date'], $article['author'],
> $article['content']
> rather than
> $id, $title, $publish_date, $author, $content
>
> olinux


I agree here. I'm new to this templating system thing, so I'm
studying on it and have found the smarty concept better. I also
found this article that I liked and gives a simple solution to
implement in few steps to get started:

http://www.sitepoint.com/article/1218

Cristian
Reply With Quote
  #10 (permalink)  
Old 09-30-2003
Rush
 
Posts: n/a
Default Re: Cleaning up my messy code

"Chris" <chrisl_ak@hotmail.com> wrote in message
news:Xns940573C7DAE53chrislak@216.92.131.4...
> I'm not looking for an IDE or code generator so much as some practical
> advice for organization and framework when developing a larger app. I know
> of PHP Fusebox, having programmed with Cold Fusion fusebox for a while,

but
> it seems like that might be too much. Maybe I just need a sensical,
> practical approach to application layout. What do you all do? How can I
> learn to be a better PHP programmer in this regard?


Try giving a TemplateTamer a shot. It is not too big, but it can help you
organize yourself better. On it's wiki you will find some examples and
tutorials.

rush
--
http://www.templatetamer.com/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:24 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0