www.webdeveloper.com
Results 1 to 3 of 3

Thread: Oreily book - PHP the good part - question about objects

  1. #1
    Join Date
    Jun 2014
    Posts
    3

    Oreily book - PHP the good part - question about objects

    In this book they have an example of a simple HTML page with a form that is implemented in OOP.
    Is this example really better than have an HTML with PHP inside?

    Here is the beginning of this code...
    PHP Code:
    class html {
    // class properties (variables) are defined first
    private $tag ;
    // next define the methods (class functions)
    function __construct ($title="") {
    // class constructor, this builds the top of the HTML page
    // each time the class is constructed
    $this->tag "<HTML> " ;
    $this->tag .= "<HEAD>" ;
    $this->tag .= "<title> $title </title>" ;
    $this->tag .= "</HEAD><BODY>" ;
    echo 
    $this->tag ;
    return ;
    }

    function 
    page_end () {
    // end of the HTML page, close the body
    // and the html tags
    $this->tag "</BODY></HTML>" ;
    return 
    $this->tag ;
    }

    function 
    RawText($textString$textColor="black"$bgcolor=''$fontSize="",
    $fontWeight="normal") {
    // this method is for sending raw unformatted text to the browser
    $this->tag "<span style='color: $textColor ; background-color: $bgcolor ;
    font-size: 
    $fontSize ; font-weight: $fontWeight'>" ;
    $this->tag .= "$textString";
    $this->tag .= "</span>" ;
    return 
    $this->tag ;
    }

    function 
    Image($source$title=""$height=""$width=""$align="center",
    $border=0$valign="middle",
    $class=""$id=""$name=""$onType1=""$onAction1="",
    $onType2=""$onAction2=""$onType3=""$onAction3="") {
    // this method is for adding images to the html page, it has
    // room for up to 3 onactions (onclick, onblur, onmouseup, etc)for
    each method call
    $this
    ->tag '<img src="' $source '" ' ;
    if (
    $name$this->tag .= 'name="' $name '" ' ;
    if (
    $height == ""$height "16" ;
    if (
    $width == ""$width "16" ;
    $this->tag .= 'height="' $height '" width="' $width '" ' ;
    $this->tag .= 'border="' $border '" ' ;
    if (
    $class$this->tag .= 'class="' $class '" ' ;
    if (
    $id$this->tag .= 'id="' $id '" ' ;
    if (
    $title$this->tag .= 'title="' $title '" alt="' .
    $title '" ' ;
    if (
    $align$this->tag .= 'align="' $align '" ' ;
    if (
    $valign$this->tag .= 'valign="' $valign '" ' ;
    if (
    $onType1$this->tag .= $onType1 '="' $onAction1 '" ' 
    Last edited by NogDog; 06-22-2014 at 08:24 AM. Reason: added [php] tags

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    548
    Without analyzing this sample of yours, the quick answer is YES - it is better to separate the presentation logic (css/html/js) from the actual (business) logic (php). For simplicity's sake, for readability, and for future maintenance. Where possible, place your dynamic content in php vars and pass them to your html area to be displayed with echo's or (to me) better yet use the heredocs feature to compose your html and those php vars quickly and easily. When necessary it is allowable (?) to use short php loops in the html area to display a series of php-provided data items (a table?) combining limited html along with the values obtained from a previously-created query result.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    As far as using PHP classes/objects for the generation of HTML, I don't use it much, myself. That doesn't mean it's necessarily better or worse -- but then I'm not a big fan of PHP template engines, either, while many developers apparently like to use them. I'm more likely to make use of a few "helper" functions to handle some of the "boiler plate" parts of the HTML output, along with liberal use of require() to pull in static parts (or non-static, for that matter) of each view.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles