#1
  1. No Profile Picture
    frequent PHP idiot
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    354
    Rep Power
    11

    Objects & html structure


    I'm new to OOP and want to make sure I'm writing cleanly. I have several classes, each for a particular piece of my application. Should I leave HTML inside these methods or should I just be call various methods within a PHP/HTML page to display the data.

    The problem I'm running into is that my methods are starting to get pretty sloppy with a mix of method calls and echoed HTML.

    Say for instance I have a class to display projects and another to display comments. Should I just create an HTML template for each class and then include those template pages?

    What's the best structure to do this?
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,066
    Rep Power
    1990
    As far as I'm concerned you should always separate the PHP and HTML (where-ever possible). You've seen that it gets messy trying to output HTML inside your classes, and it only gets worse as things get more complex.
  4. #3
  5. Banned
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,938
    Rep Power
    0
    Hi,

    you should use a dedicated template engine like Twig or Smarty.

    Mixing PHP and HTML (and SQL and JavaScript and CSS and whatnot) does not only create a mess. It also couples the visual presentation with the application logic, so whenever you wanna make a small change in the GUI, you have to fumble with the application code. This wastes time, makes things unnecessarily complicated and always comes with the risk of breaking something -- pretty much the last thing you want.

    Unfortunately, PHP is more or less based on the idea of writing multilingual spaghetti code, so you need a lot of discipline and good libraries to untangle the languages again. But it's possible.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,699
    Rep Power
    6351
    The MVC design pattern in particular keeps your "view" code (HTML) completely separate from your "model" and "controller" code. There are a number of ways to separate your PHP code from your view code. In increasing order of "correctness" (#1 is most correct):

    1) Separate languages. You can use javascript, XSLT, or a specialized templating language like twig to print the HTML. This forces you to prepare the data beforehand in the logical portion of the code.

    2) Separate PHP output files. Use PHP for its intended purpose: formatting web pages. But don't mix that inside the logical/backend PHP code. Make the PHP template file work on arrays, objects, etc. Make a code review rule (or a personal one) so that no business logic or excessive formatting makes it into the template code. While this is the most powerful solution, it's also the most tempting to take shortcuts.

    3) Special outputting functions. You can make it a rule that each PHP function has a corresponding "output" function, separating your logical functions from the corresponding output function, so you can change the logic without worrying about changing the output, and vice versa. This is the sloppiest and laziest of the methods, and will make your code hard to maintain.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo