Results 1 to 5 of 5

Thread: object oriented PHP

  1. #1
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face object oriented PHP

    hie there
    i've been reading about classes and inheritance in php.
    i copied the following codes that implements the use of classes and inheritance but could not work.No error is being displayed when view the out from the browser but a blank page only.

    the codes are as follows:

    here is the code for the class named page.inc

    <?php
    class Page
    {
    // class Page's attribute
    var $content;
    var $title="TLA Contsulting Pty Ltd";
    var $keywords="TLA Constulting,Three Page Abbreviation,some of my best friends are search engines";
    var $buttons=array("Home"=>"home.php",
    "Contact"=>"contact.php",
    "Services"=>"services.php",
    "Site Map"=>"map.php");
    // class page's Operation

    function SetContent($newcontent)
    {
    $this->content=$newcontent;
    }

    function SetTitle($newtitle)
    {
    $this->title=$newtitle;
    }
    function SetKeywords($newkeywords)
    {
    $this->keywords=$newkeywords;
    }
    function SetButtons($newbuttons)
    {
    $this->buttons=$newbuttons;
    }
    function Display()
    {
    echo "<html>\n<head>\n";
    $this->DisplayTitle();
    $this->DisplayKeywords();
    $this->DisplayStyles();
    echo"</head>\n<body>\n";
    $this->DisplayHeader();
    $this->DisplayMenu($this->buttons);
    echo $this->content;
    $this->DisplayFooter();
    echo "</body>\n</html>\n";
    }
    function DisplayTitle()
    {
    echo "<title>$this->title</title>";
    }
    function KeyWords()
    {
    echo "<META name=\"keywords\" content=\"$this->keywords\">";
    }
    function DisplayStyles()
    {
    ?>
    <style>
    h1{color:white;font size:24pt;text-align:center;
    font-family:arial,sans serif}
    .menu{color:white;font-size:12pt;text-align:center;
    font-family:ariel,sans serif;font-weight:bold}
    td{background:black}
    p{color:black;font-size:12pt;text-align:justify;
    font-family:ariel,sans serif}
    p.foot{color:white;font-size:9pt;text-align:center;
    font-family:ariel,sans serif;font-weight:bold}
    a:link,a:visited,a:active{color:white}

    </style>
    <?php

    }
    function DisplayHeader()
    {
    ?>
    <table width="100%" cellpadding="12" cellspacing="0" border="0">
    <tr bgcolor="Black">
    <td align="left"><img src="logo.gif"></td>
    <td>
    <h1>TLA Consulting Pty Ltd</h1>
    </td>
    <td align="right"><img src="logo.gif"></td>

    </tr>

    </table>

    <?php
    }

    function DisplayMenu()
    {
    echo "<table width=\"100%\" bgcolor=white"
    ."cellpadding=4 cellspacing=4>\n";
    echo"<tr>\n";

    // Calculate Button size
    $width=100/count($buttons);

    while (list($name,$url)=each($buttons))
    {
    $this->DisplayButton($width,$name,$url,!$this-IsURLCurrentPage($url));

    }
    echo "</tr>\n";
    echo "</table>\n";

    }
    function IsURLCurrent($url)
    {
    if (strpos($GLOBALS["SCRIPT_NAME"],$url)==false)
    {
    return false;
    }
    else
    {
    return true;
    }


    }
    function DisplayButton($width,$name,$url,$active=true)
    {
    if($active)
    {echo"<td width=\"$width%\">
    <a href =\"$url\">
    <img src =\"s-logo.gif\" alt=\"$name\" border=0></a>
    <a href=\"$url\"><span class menu >$name</span></a></td>";
    }
    else
    {
    echo "<td width=\"$width\">
    <img src =\"side-logo.gif\">
    <span class menu>$name</span></td>";
    }

    }
    function DisplayFooter()
    {
    ?>
    <table width="100%" bgcolor="Black" cellpadding="12" border="0">
    <tr>
    <td>
    <p class="foot">&copy;TLA Consulting Pty Ltd.</p>
    <p class="foot">Please See<a href="">Our Legal Information Page</a> </p>
    </td>
    </tr>
    </table>
    <?php
    }
    }
    ?>


    then for the home page save as home.php the code is as follows:

    <?php

    require("page.inc") ;


    $homepage=new Page();

    $homepage->SetContent("<p>Welcome to the home os Consulting.
    Please take some time to get to know us .</p>
    <p>We specialize in serving your bussines needs and hope to hear from you soon.</p>");
    $homepage->Display();
    ?>


    then for the class named ServicesPage.php the codes are:

    <?php
    require("page.inc");

    class ServicesPage extends Page
    {
    var $row2buttons=array("Re-engineering"=>"reengineering.php",
    "Standard Compliance"=>"standards.php",
    "Buzzword Compliance"=>"buzzword.php",
    "Mission Statements"=>"mission.php");
    function Display()
    {
    echo "<html>\n<head>\n";
    $this->DisplayTitle();
    $this->DisplayKeywords();
    $this->DisplayStyles();
    echo "</head>\n<body>\n";
    $this->DisplayHeader();
    $this->DisplayMenu($this->buttons);
    $this->DisplayMenu($this->$row2buttons);
    echo $this->content;
    $this-DisplayFooter();
    echo "</body>\n</html>\n";

    }
    }
    $services=new ServicesPage();
    $content="<p>At TLA Consulting ,we offer a number of services.
    Perhaps the productivity of employees would improve
    if we reengineered your business.
    Maybe all your bussiness needs is fresh mission
    statement ,or a new batch of buzzwords.";
    $services->SetContent($content);
    $services->Display();
    ?>



    please help me!!
    thanks in advance

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Can we see page.inc?

    You can also change:
    PHP Code:
    var $content;
    var 
    $title="TLA Contsulting Pty Ltd";
    var 
    $keywords="TLA Constulting,Three Page Abbreviation,some of my best friends are search engines";
    var 
    $buttons=array("Home"=>"home.php",
    "Contact"=>"contact.php",
    "Services"=>"services.php",
    "Site Map"=>"map.php"); 
    To:

    PHP Code:
    var $content$title="TLA Contsulting Pty Ltd"$keywords="TLA Constulting,Three Page Abbreviation,some of my best friends are search engines"$buttons=
    array(
    "Home"=>"home.php",
    "Contact"=>"contact.php",
    "Services"=>"services.php",
    "Site Map"=>"map.php"); 
    Jeremy | jfein.net

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Nile View Post

    You can also change:
    PHP Code:
    var $content;
    var 
    $title="TLA Contsulting Pty Ltd";
    var 
    $keywords="TLA Constulting,Three Page Abbreviation,some of my best friends are search engines";
    var 
    $buttons=array("Home"=>"home.php",
    "Contact"=>"contact.php",
    "Services"=>"services.php",
    "Site Map"=>"map.php"); 
    To:

    PHP Code:
    var $content$title="TLA Contsulting Pty Ltd"$keywords="TLA Constulting,Three Page Abbreviation,some of my best friends are search engines"$buttons=
    array(
    "Home"=>"home.php",
    "Contact"=>"contact.php",
    "Services"=>"services.php",
    "Site Map"=>"map.php"); 
    I wouldn't. How he has it is much easier to read and is the stardard way to code. I would however use public, protected, or private instead of var. And I would add some tabs or spaces and some line spacing too.

    terencedangwa,

    The code you posted is too hard to read. Clean it up and put it inside php tags. Better yet, if what you posted is actually in different files, use separate PHP tags for each file and put the file name at the top. Look in the menu above the textarea of your post and click on the php icon to insert tags into your post.

  4. #4
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    I believe your code came from the book "PHP and MySQL Web Development". That being the case, I did notice that you deviated a little bit from the code provided by the book. In page 140 the author says:

    "The require( ) statements in home.php load header.php and footer.php. As mentioned previously, the name given to these files does not affect how they are processed when you call them via require( ). A common convention is to call the partial files that will end up included in other files something.inc (here, inc stands for include).This is not recommended as a general strategy, as .inc files will not be interpreted as PHP code unless the web server has been configured specifically for this."

    I would suggest making all your files .php instead of .inc. Personally I prefer writing all my CSS and JavaScript code in external files unless the code is going to be used in only one page. It took me a few days and some research on the W3C and other web sites, especially to wrap my head around the "$this->" and "__get( )" concepts, but eventually the sky cleared and the sun shined through. I copied the code from the book for both files and placed them on a temp folder on my site and it worked as intended. I would suggest that you, like I had to do, follow the code in the book for now until you get a stronger grasp on these concepts. Also, I do agree with Jason and Nile, I would rather use "public" rather than "var". Just my two cents!

    http://www.xtianos.com/test/home.php

    PHP Code:
    <?php
        
    require('page_class.php');
        
    $newpage = new page_class();
        
    $newpage -> pagetitle "C M and The Girls Page";
        
    $newpage -> pageheading "Welcome to the C & M Page";
        
    $newpage -> contents "
                    \t<p>The family photos include pictures of the girls (Abi, Gabi and Iza) along with some of ours.  The new house photos are of the house we just                     purchased, our first house ever!  We are excited about the purchase, although not about the mortgage, but I guess every level has its devil.  The                     recipes are miscellaneous cooking, deserts and drinks recipes that we have gathered from various sources (i.e., magazines, internet and word of                 mouth) so please feel free to copy them and use them.  Lastly, we included some information about vitamins and minerals along with their uses and                     benefits because we thought you might benefit from knowing and maybe using them.</p>\n
                    \t<p>Finally, we hope you enjoy our page and please if you find any link or button that does not work properly let us know so we can fix it.  Thanks                     again and we hope you come back soon!  Cheers!</p>\n"
    ;
        
    $newpage -> displaypage();
    ?>
    PHP Code:
    <?php

        
    class page_class{
            public 
    $contents;
            public 
    $pageheading;
            public 
    $pagetitle;
            public function 
    __set($a,$b){
                
    $this -> $a $b;
            }
            public function 
    opening1(){
                print 
    "<!DOCTYPE html PUBLIC \"-//W3C/DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml/DTD/xhtml11-transitional.dtd\">\n";

                print 
    "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";

                print 
    "<head>\n";

                print 
    "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n";
            }
            public function 
    css(){
                print 
    "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/default.css\" />\n";
            }
            public function 
    javascript(){
                print 
    "<script type=\"text/javascript\" src=\"../scripts/default.js\"></script>\n";

                print 
    "<script type=\"text/javascript\">\n";

                print 
    "\tif(top != self){\n";

                print 
    "\t\ttop.href = \"http://www.yahoo.com\"\n";

                print 
    "\t}\n";

                print 
    "</script>\n";
            }
            public function 
    opening2(){

                print 
    "<title>".$this -> pagetitle."</title>\n";

                print 
    "</head>\n";

                print 
    "<body>\n";
            }
            public function 
    navigationbar(){
                print 
    "\t<div class=\"center italic\">\n";
                print 
    "\t\t<h3>".$this -> pageheading."</h3>\n";
                print 
    "\t</div>\n";
                print 
    "\t<div class=\"navbuttons center\">\n";

                print 
    "\t\t<ol>\n";

                print 
    "\t\t\t<li><a href=\"pages/familyphotos.php\">Family Photos</a></li>\n";

                print 
    "\t\t\t<li><a href=\"#\">Recipes</a></li>\n";

                print 
    "\t\t\t<li><a href=\"#\">Supplements</a></li>\n";

                print 
    "\t\t</ol>\n";

                print 
    "\t</div>\n";
            }
            public function 
    pagecontents(){
                print 
    $contents;
            }
            public function 
    closing(){

                print 
    "</body>\n";

                print 
    "</html>\n";

            }
            public function 
    displaypage(){
                
    $this -> opening1();
                
    $this -> css();
                
    $this -> javascript();
                
    $this -> opening2();
                
    $this -> navigationbar();
                print 
    $this -> contents;
                
    $this -> closing();
            }
        }
    ?>
    Last edited by xtiano77; 02-22-2009 at 08:04 PM.

  5. #5
    Join Date
    Feb 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hie guys
    thanx for your contribution
    i'll come back to you soon.
    let me take this opportunity to analyze your ideas.

Bookmarks

Posting Permissions

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