Home arrow PHP arrow Page 6 - JV’s Power Tips for PHP (1)
PHP

JV’s Power Tips for PHP (1)


In this article Justin provides some tips for improving our PHP applications such as a very simple but effective template technique.

Author Info:
By: Justin Vincent
Rating: 3 stars3 stars3 stars3 stars3 stars / 3
January 29, 2003
TABLE OF CONTENTS:
  1. · JV’s Power Tips for PHP (1)
  2. · Never Breaking Out of PHP
  3. · Basic Template Theory
  4. · Refining the Previous
  5. · Accessing Other Variables from Within this Type of Template
  6. · Using Objects to Store Multiple Output Values
  7. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JV’s Power Tips for PHP (1) - Using Objects to Store Multiple Output Values
(Page 6 of 7 )

Another reason why this is useful is that it files your output variables into one (or a few) objects. It also means that in our function get_template() we can get rid of this:

<?php
    // etc.
    
    // Make sure that all global variables are available to this functions..
    foreach ( $GLOBALS as $key => $val )
    {
      global ${$key};  
    }

    // etc.

?>


...which, in truth, is a little costly and replace it with this:

<?php
    // etc.
    
    // We only need to make the object $output global to this function..
    global $output;

    // etc.

?>


Going back to our table template. It would look like this:

<table>
  <tr>
    <td>
      $output->dynamic_text
    </td>
  </tr>
</table>


Of course, you don’t have to write your own template system if you don’t want to. I just happen to be a control freak who likes to know the structure of all the code I use. There are plenty of pre-built template systems out there that can save you hours of coding (e.g. Smarty). Even so, the principle is still the same:
  1. Don’t break out of PHP tags.
  2. Abstract HTML content from programming logic.

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials