Jump to content

General coding structure and ideology


Ninjakreborn

Recommended Posts

Ok, I am starting to see multiple way's of programming.
I have my own style's (Granted) I don't like anyone putting down my personal style of programming obviously.  However I have seen some specific style's I like, some I specifically find disgusting.  I know it's a matter of opinion.
However I am trying to decide on.
One way I didn't like was this obsessive php mode. 
<?php
// echo everything on the page
?>
I personally like having some xhtml, some php in different places.
One thing I have noticed, when I decide that I jsut want to echo php, or larger portion of echo's.  Echoing the xhtml I mean.  I sometime's notice people put more of it in a variable.
I am wondering, is that a good way, I am starting to like it.
for instance.  Someone might have.

echo "<a href="#"><img src=\"/oakley/{$image_row[pathtofile]}{$image_row[filename]}\" alt=\"{$image_row[filename]}\" height=\"70px\" width=\"\" /></a>";
Ok this is a total echo of the whole thing, no questions asked.

Then here, I saw someone do this

echo "<a href="#"><img src=\"/oakley/{$image_row[pathtofile]}{$image_row[filename]}\" al
well you get the picture, instead of havingt alt=""
he would have $alt
and he would have alt in a variable like
$alt = "whatever"
then pass that through the echo
I use to do that
then I saw someone with total variable
[code]<?php
$firstpart = "<a href=\"#\">";
$height = "height=\"70px\"";
$width = "";
$middlepart = "<img src=\"/oakley/{$image_row[pathtofile]}{$image_row[filename]}\" alt=\"{$image_row[filename]}\" {$height} {$width} />";
$lastpart = "</a>\n";
echo $firstpart . $middlepart . $lastpart;
?>[/code]
When I saw that, I was unsure if I liked that better, or if I liked instead, having the echo natural, then having just some of the attribute's to some of the tag's as variable's to make the code easier to edit?  Which way do you prefer, or do you have some alternative way's
Link to comment
Share on other sites

There's no best way.. I suggest you adjust your style according to the circumstances.  If it looks more readable to put more information into each variable, then do it.  If it doesn't, then don't.  The most important thing to consider is maintainability.  When you want to change that code later, what style will make those changes easier?  And that style may be different for different parts of your script.

At my workplace we use Smarty templates for everything now.  For large projects, it's the best solution.  For small projects, the methods you described work just fine.
Link to comment
Share on other sites

My personal preference is to use a template class that stores all needed data in a single array, I then parse an HTML markup template which will have markers:

[code]<div class="div1"><?php echo $this->_data['div1']; ?></div>[/code]

The data provided to the template engine will have no markup whatsoever. Only my templates have markup in them.
Link to comment
Share on other sites

i'm with Jenk on this one, although I would not recommend Smarty at all, mainly due to its whole new syntax, not just its bulk . btherl is right in saying also there is no best way, but i certainly think there are best 'ways'.

i must admit i do have a real problem with the whole echo"<div>hello world</div>" style of coding, mainly because it makes things such an arse to fix if you need to tweak a layout. not on about seperation/MVC/templating here, I just reckon that there are so many ways to code that avoid this approach that there isn't really an excuse to totally form a layout using 'echo'. Using HTML inside PHP in my opinion is worse than using PHP inside HTML.

until relatively recently, I generally used Dreamweavers templating. However, this still meant hard to maintain  files as I'd have a HTML file with loads of PHP stuck at the top. updating a site, or getting someone to update it, generally meant using Dreamweaver - not everyone has that.

So then I found this: http://www.massassi.com/php/articles/template_engines/
There's literally enough code on there to form a basic templating class which works, just so that you can "nail" that particular type of coding and play around with it. The goal here is to keep your HTML/PHP seperate, so you're either dealing with PHP, HTML and not one bit mash-up of both. My own templating "engine" is based on that, albeit with tonnes bolted on as I've needed it along the way.

When using templates, I'm also a fan of the "alternative" php syntax, using things like:

[code]
<html>
<body>
  <h1>This is my <?=$listname ?> list</h1>
  <ul>
      <? foreach ($posts as $post): ?>
      <li><?=$post['title'] ?></li>
      <? endforeach; ?>
  </ul>
</body>
[/code]
using short tags, etc as well. This kinda keeps the "tags" similar and simple like other template systems like Smarty and is the way Ruby on Rails does it - using the native code rather than a whole new syntax. Some people don't like this way as it can be turned off, but as the default installation has it switched on (or at least that's what I've found so far) and PHP6 is keeping these short tags, I don't really see too many issues - especially as everyone seems to be trying to be "Ruby on Rails" about what they do these days.

After all that waffle, I guess my point is - keep your HTML and your business logic seperate. This way you can avoid the need to have echo's all over the place. My new framework literally has a single echo in the entire thing - to dump the final rendered page to the screen. If I want to redraw a template, I don't break my application. If I want to rewrite my application in a different way/style, etc, I don't break my templates.

Ok, so all of that is advocating using MVC/templates rather than your original question of coding style, etc, but I'm sure you can see it all helps keep things tidy, which is the whole point of worrying about clean code in the first place.

Cheers
Mark
Link to comment
Share on other sites

I guess it is time for me to start looking into that.  MVC patterns.
I have been avoiding it, also I have been avoiding classes/objects.
I have not been doing functions too long, with my framework, but maybe it's not a bad idea to go ahead and get things organized.  Thanks for all the advice, that was quite a bit.
Link to comment
Share on other sites

as i've said before, look at CodeIgniter or Cake, especially the former. The documentation alone will give you a nice (laymans-ish) crash course in MVC, and just taking a look at a sample application will give you the idea of how structured sites are much easier to maintain.
Link to comment
Share on other sites

About php, I know a lot, the thing's I am starting to look into now, are things I see other people talking about.
Object Oriented Programming, my experience level with PHP in general is rather high.
I can program a high end, backend with little effort, because I know php/mysql to the point of being good at it.
The things I am focusing on looking up/learning now are
1. Object Oriented Programming
2. I already built my framework, I am looking into making it more like a templating engine here pretty soon.
3. The 2 types of patterns that were spoken about, don't remember hte letter's for them now, but I am looking into those (one point of input and one point of output) I have been checking into that

Those are the things I have been working on lately.
Link to comment
Share on other sites

My advice: focus on nr 1 on that list.

And I don't just mean learning php OOP syntax, but learning how to apply OOP principles in php.

Your question made me realize the resources in the sticky don't cover those basics. I'll hunt for some resources for you. Read, a lot. Then if you have questions, you are of course more than welcome to post them.

Links underway.

On a sidenote:

[quote=busi]2. I already built my framework, I am looking into making it more like a templating engine here pretty soon.[/quote]

A framework is not a template engine, nor the other way around.

A template engine CAN be part of a framework. As Mark pointed out, because it is possible to nest "presentation logic" in html files, you don't even absolutely need a template engine to get apt separation between business and presentation logic. But that is more a matter of preference than fact. Read the article he proposed in the resources sticky if you want more info on this subject.

I wouldn't recommend using smarty either. I am in agreement with Mark that it is too much trouble for you right now, simply because you will want to invest time in grasping OOP first.

However, if you are insisting on using a template engine, I'm gonna support Jenk's suggestion that you DO use smarty, rather than create your own, if only to get an idea of how template engines work.
Link to comment
Share on other sites

Then I am definitely confused.  I alway's thought I am totally confused on object oriented programming.  No matter what anyone say's, I took people's advice and dropped graphic design, and got a friend, that has a lot of experience to do it for me, actually with a sliding scale payment arrangement, (I don't pay him, I work it off through programming, and he end's up owing me anyway.  So because of that I feel that I haev gotten a lot better at programming, with not only PHP but with general programming,  And other languages as well seem to be all the same ocncepts as I go along.
This is what I think each thing meant.
1. Object Oriented Programming - I can never get a grasp to it's meaning because every person, and every tutorial relates it to real life.  In a program I am writing how am I going to make a dog, and make it sit, lay down, take a shit, and walk.  But so far everyone I have asked, has used dog, or cat, or person as an analogy.  Everytime you create a new instance of an object, you create a new instance of that dog, and it's a little hard to follow.  I don't have use for dog's and cat's in my programming, so what can I think about to help me understand the concept, because even tutorials I have read tries to explain it from a real life perspective.
2. patterns - mvc and vcp I think was what you called htem.  I am going to look up on them, so far I am thinking they are a specific pattern (style) to programming.  This "one point of input one point of output" sounds like you only allow one form to bring information into the page, and you make everything on the site at the end go into one huge variable and echo it onto the page.
What I don't know about that is, when I try to edit third party script's sometimes when clients ask, I go into it and try to modify the code, I always figure it out, but it take's forever I am guessing it's because they use OOP.
3. Templating engine - i thought this was a framework.  I have something that allows me to have access to all my function's, as well as work have all of my stuff together, like database login information, session information or whatever else I think i'll need.  I thought a templating engine would be like a replicateable way to build site's.
Like I use the docroot variable now, I thought that would bes omething that would be found in a templating engine.
Link to comment
Share on other sites

Yeah, OO is pretty tricky in that regard.  If you were programming a game, then those examples would make more sense.  When I was at UNH, we basically used the OO capabilities of C++ to create our own abstract data types/structures.  For PHP, I think that's a better way to look at it.  A C++ OO example would be a dynamically allocated list, queue, or stack.  Or maybe an improved string data type.

So, don't worry so much about what the examples create.  The important thing is knowing how classes are created and how you can use them.  They're basically just a way to make your own data structures.
Link to comment
Share on other sites

Regarding HTML / PHP separation, for anything more than a line or two, I tend to use the HEREDOC syntax, as it gives me variable interpolation without the need to escape double-quotes.  Obviously, if I don't need variables, I'll just drop out of PHP for a bit.

Template-wise, I'm using the template class from phpBB.  It's very lightweight and perfect for what I need.
Link to comment
Share on other sites

Separation is more than just using appropriate syntax to separate, it's more along the lines of "What happens if you want the data, but not in HTML?"

If you are using embedded markup, you'll have potentially LOTS of editing to do. Where as in a properly seaparated application, you would only need to write and extra view (template) and you are done.

As for comparing real life to OO, this is a problem everyone has so don't be alarmed.

Here is an example that you should be able to relate to.. an array.

Instead of:
[code]<?php

$array = array(1,2,3,4);

$keyOf2 = array_search(2, $array);

shuffle($array);

foreach($array as $key => $val)
{
    echo $key . ' => ' . $val . chr(10);
}

$randomValue = $array[array_rand($array)];

?>[/code]

Where every function is 'external' you would have something like:

[code]<?php

$array = new MyArrayObject(1,2,3,4);

$keyOf2 = $array->search(2);

$array->shuffle();

while ($array->hasNext())
{
    echo $array->getKey() . ' => ' . $array->getVal();
}

$randomValue = $array->randVal();

?>[/code]

You may not see the immediate benefit of this, especially in PHP, but having an object handle it's own properties is much better than using external functions for everything. This can be particularly beneficial in applications that use namespaces extensively (i.e. just about anything other than PHP) so you don't have to import, or define many namespaces. (think Java and "import java.io.*;" etc)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.