Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts

    Website Template (PHP OOP mixed with HTML)

    I've been playing with OOP for a while, and now i figured out good way to display website with it's main elements, easily.
    Want to share the class with the world, so here it is...

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <?php
    class Elements {
        public function 
    __construct($charset) {
            
    $this->charset $charset;
        }
        public function 
    title($sharedinfo) {
            
    $sharedinfo "<html>\r\n<head>\r\n<title>$sharedinfo</title>";
            return 
    $sharedinfo;
        }
        public function 
    charset($charset) {
            
    $charset "\r\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=$charset\" />";
            return 
    $charset;
        }
        public function 
    metadescription($sharedinfo) {
            
    $sharedinfo "\r\n<meta name=\"description\" content=\"$sharedinfo\" />\r\n</head>\r\n<body>\r\n";
            return 
    $sharedinfo;
        }
        public function 
    content($sharedinfo) {
            return 
    $sharedinfo."\r\n</body>\r\n</html>";
        }

        public function 
    results($sharedinfo) {
            
    $title                $this->title($sharedinfo);
            
    $charset            $this->charset($this->charset);
            
    $metadescription    $this->metadescription($sharedinfo);
            
    $content            $this->content($sharedinfo);
            
    $results             $title.$charset.$metadescription.$content;
            return 
    $results;
        }
    }
    class 
    Website  {
        public 
    $charset;
        public function 
    __construct($charset) {
            
    $this->elements = new Elements($charset);
            
    $this->charset $charset;
        }
        public function 
    fullpreview($sharedinfo) { 
            return 
    $this->elements->results($sharedinfo);
        }
        public function 
    testcharset() {
            return 
    htmlspecialchars($this->elements->charset($this->charset));
        }
        public function 
    partialview($getresults$sharedinfo) {
            return 
    htmlspecialchars($this->elements->$getresults($sharedinfo));
        }
    }

    $website = new Website("utf-8");
    print 
    $website->fullpreview("Template 1");


    /*print "<h1>This is full code generated by website</h1>";
    print "<blockquote><pre><h2>";
    print $website->partialview("title", "Template Site");
    print $website->testcharset();
    print $website->partialview("metadescription", "Template Site");
    print $website->partialview("content", "Template Site");
    print "</h2></pre></blockquote>";*/
    ?>
    If you uncomment last piece of code, you can see full HTML generated by this class. Btw, PHP 5 or more is necessary to run this script (constructs are not suported in PHP4 last i checked)

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by funnymoney View Post
    (constructs are not suported in PHP4 last i checked)
    Constructs are supported in PHP 4. They take on the name of the class itself.

    Naming the constructor __construct is not supported in PHP4 .

  • Users who have thanked kbluhm for this post:

    funnymoney (07-24-2009)

  • #3
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by kbluhm View Post
    Constructs are supported in PHP 4. They take on the name of the class itself.

    Naming the constructor __construct is not supported in PHP4 .
    wish i knew that few months ago when i had to force their host to change to PHP 5 couse program didn't work on 4 . but anyway, it's better to use PHP5 in many ways than 4 so it's not that bad like it could have been

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by funnymoney View Post
    wish i knew that few months ago when i had to force their host to change to PHP 5 couse program didn't work on 4 . but anyway, it's better to use PHP5 in many ways than 4 so it's not that bad like it could have been
    Agreed 100%. No need to support PHP 4 any longer.

  • #5
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by kbluhm View Post
    Agreed 100%. No need to support PHP 4 any longer.
    well it's now and then about php 4 or 5 to programmers, but, when you commented on that part, do you have any good ideas about this code snippet of mine... i know it's not much, but.. for beginners it can help them understand some tricks of OOP.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by kbluhm View Post
    Agreed 100%. No need to support PHP 4 any longer.
    Ehhhh, I'm kinda on the fence about agreeing.
    I would never write something of my own for PHP4. However, I do stand by my own rule of supporting 1 version in the past if necessary, so I'll finally be able to nuke the PHP4 out when 6 is released. Good o'l days, PHP4 brought us alot of awesome.

    For something you're letting people download (not custom), definitely don't support PHP4. This is where I agree completely. If people want it in PHP4, they can either pay for the custom or convert it themselves.

    BTW, the classes would be really easy to convert to PHP4 since it doesn't have any interfacing, binding, exceptions, or necessary scoping which are all features PHP brought in with the introduction of PHP5. Except PHP5.0.1 - OOP was terrible. Simply dropping the 'public' from any method, change __construct to match the classname, and change the member scope from public to var and its good to go. Still work in php5 as well, no BC Break on that. Though that kinda defeats PHP5 :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by Fou-Lu View Post
    ...the classes would be really easy to convert to PHP4 since it doesn't have any interfacing, binding, exceptions, or necessary scoping which are all features PHP brought in with the introduction of PHP5...
    do you think this classes could use some of the stuffz you mentioned there (interfaces and those other.. )

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No the only thing that would really apply for this is to change you're Website class to private instead of public and declare the members inside of the Element.
    If you're looking to expand and create a more extensive system, Interfaces are the way to go (which will inherit binding just by itself), even though an unrecognized bug exists with private scoping in PHP, it is mostly un-noticable (Interfaces are not actually reliable in PHP since there is a way to override the scope of an interfaced method making it unacccessable which causes a fatal error - this is because parent methods actually leak their private methods and variables into the child classes).
    Although this would be neat and put you're pages together easy:
    PHP Code:
    $site = new Website();

    $body $site->addElement(new Body());
    $paragraph $body->addElement(new Paragraph());
    $paragraph->addElement(new Text('This is a paragraph'));

    print 
    $site->display(); // Or you can overload the __toString and just print $site. 
    You'll notice this example alone has four classes in use, Website, Body, Paragraph and Text. The downside, you'll need a class for pretty much any element you want to provide specific rulesets for. The plus, you know exactly what is here and where it is.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    funnymoney (07-28-2009)

  • #9
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by Fou-Lu View Post
    If you're looking to expand and create a more extensive system, Interfaces are the way to go ...

    ..... The downside, you'll need a class for pretty much any element you want to provide specific rulesets for. The plus, you know exactly what is here and where it is.
    Thanks for the input. I am trying to build a bit more extensive system than ones i did before. You suggested creating class for any element, which is not bad idea, but problem is in loading those classes to website.

    I'm building system that will allow me to create websites faster than before. But i don't know how to load all elements or classes based on request made by get or post values...

    It's not a problem to load default elements like title, metadescription or even body itself, but when like i'm creating now, certain body requires certain additional modules to be loaded inside it can be a problem.

    here is how i started it...

    PHP Code:
    <a href="?language=English">English</a> <a href="?language=Croatian">Croatian</a>
    <br>
    <?php
    define 
    ("DEFAULT_GET""language");
    define ("DEFAULT_PAGE""English");

    $languages     = array(DEFAULT_GET => array("English","Croatian"));

    function 
    preload($values) {
        foreach (
    $values as $key=>$a) {
            if (isset(
    $_GET[$key])) {
                
    $_GET[$key]     = preg_replace("/(#s)*(%)*(\+)*/"""htmlspecialchars(urlencode($_GET[$key])));
                
    ##ADD SECURITY HERE
                
    $page $_GET[$key];
            }
            else {
                
    $page DEFAULT_PAGE;
            }
        }
        return 
    $page;
    }

    function 
    loadModules($language$values$i) {
        if (
    $i count(array_values($values[$language]))) {
            
    ###RECURSIVE NEW OBJECT FOR MODULE;
            
    $modules    = new Modules($language);
            print 
    $modules->$values[$language][$i]();
            
    loadModules($language$values$i+1);
        }
    }

    $language    preload($languages);

    $English     = array    ("English"         => array("module1",     "module2"                ));
    $Croatian     = array    ("Croatian"     => array("module2",      "module1",    "module3"    ));

    loadModules($language, $$language0);

    class 
    Modules {
        public 
    $language;
        
        public function 
    __construct($language) {
            return 
    $this->language $language;
        }

        public function 
    module1() {
            return 
    "Language {$this->language} is calling module1...<br>";
        }
        public function 
    module2() {
            return 
    "Language {$this->language} is calling module2...<br>";
        }
        public function 
    module3() {
            return 
    "Language {$this->language} is calling module3...<br>";
        }
    }



    /*
    ?>
    only question that i might add is where i recursively create new object on each recursion, but each time i create another instance of that object. to me it is waste of object creation time.

    i could do that outside recursion, but then it would be a waste of "print" keywords...

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If you're module's are also classes, you can create a module factory. For this, you will want to read some configuration data, and construct based on what is requested for a module.
    See: http://en.wikipedia.org/wiki/Factory_method_pattern and follow through to the AbstractFactory pattern to take a look at that one too. I don't think either have a PHP example, but PHP's OOP appears to follow quite closely to the Java model, minus the datatype handling. Also, PHP doesn't support the enum type, which does make it a little more open for what you can and cannot allow. Writing an enum is pretty annoying, but can be done with simulation if you generate classes on the fly for it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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