SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Webdesign with OOP

    Hello all,

    I want to learn how to make websites by using OOP.

    This is what I have now, this is a good start or have I misunderstood the structure of OOP?

    index.php
    PHP Code:
    <?php
        
    require './cls/website.php';
        
    $website = new website;
        
    $website->title 'My title';
        
    $website->content 'My content';
        echo 
    $website->render();
    ?>
    website.php
    PHP Code:
    <?php
        
    class website {
            var 
    $title;
            var 
    $content;
            
            function 
    header() {
                
    $header[] = '<!doctype html>';
                
    $header[] = '<html lang="en">';
                
    $header[] = '<head>';
                
    $header[] = '<title>'.$this->title.'</title>';
                
    $header[] = '</head>';
                return 
    $header;
            }
            
            function 
    content() {
                
    $content[] = '<body>';
                
    $content[] = $this->content;
                
    $content[] = '</body>';
                return 
    $content;
            }
            
            function 
    footer() {
                
    $footer[] = '</html>';
                return 
    $footer;
            }
            
            function 
    render() {
                
    $page array_merge($this->header(), $this->content(), $this->footer());
                foreach (
    $page as $value) {
                    echo 
    $value"\n";
                }
            }
        }
    ?>
    Could you guys give me some advice?

    Thanks in advance & sorry for my bad english...
    x-taste

  2. #2
    SitePoint Member dennis.g's Avatar
    Join Date
    Jun 2012
    Location
    Athens, Greece
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems fine to me. But not necessary. One of PHP's advantages compared to Java is the ability to let the programmer decide to work with or without objects, depending on the needs.

    It is very useful to learn how to work with objects, but remember that your project requirements is what matters. In small projects a few simple lines of code are usually enough. The larger the project gets, the more you need OOP.

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why do you want to learn to build a website using OOP?

    The reasons to use OOP is one of two: You enjoy over-complicating a project simply for the joy of it. Or you are building a flexible and powerful framework that can handle multiple types of websites/services and requires many layers of abstraction.

    If it's #1, then go for it. If it's #2, you're better off using a third party framework as they will do far more and better (and most importantly in less time) at that job than what any one person could put together.

    It's one thing to use objects in some situations as they have clear advantages over procedural code with global functions, it's another to try to force yourself to use objects when it doesn't make a lot of sense simply for the sake of saying it's OOP.

  4. #4
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    I agree with the above. Unless you are wanting to separate some form of complex business logic from display logic, and so on, I see no real need to use oop. However, if you still want to do this as a learning excercise, then why not.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your responses!

    The reason that I want to use OOP is because of two reasons:
    First off, I have about 40 websites to maintain for a client.
    All those websites use the same database, the same options and the only thing that varies - and has to vary (a lot) - Every site, is the layout.
    At first, I created a hyper-dynamic site but it got complex because of the amount of code, OOP does the trick for me.

    The second reason is also the reason that I don't use already-made products. I love to code and I want to get good at it.
    I still have much to learn and I won't learn if I only use already-made products. So, that's why... maybe silly, but I like it ^^

    If you have any futher suggestions please respond

    Thanks again,
    x-taste

  6. #6
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    The article From Flat PHP to Symfony2 may help you learn, step by step, how and where you can benefit from OOP.

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll read it.

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have one more question.
    I don't know if I should open a new thread or not, so I'll first post it here.

    I wanted to check what was faster, arrays or adding strings to an existing variable so I made this script:
    Code:
    <style>
    	span {
    		display:none;
    	}
    </style>
    
    <?php
    	for ($j = 0; $j < 10; $j++) {
    		$time_start = microtime(true);
    		
    		for ($i = 0; $i < 1000; $i++) {
    			$str = '<span>this';
    			$str = $str.'world';
    			$str = $str.'isdoomed</span>';
    			echo $str;
    			unset($str);
    		}
    		
    		$time_end = microtime(true);
    		$time = $time_end - $time_start;
    		echo '<strong>', $time, '</strong><br>';
    		
    
    		
    		
    		
    		$time_start = microtime(true);
    		
    		for ($i = 0; $i < 1000; $i++) {
    			$str[] = '<span>this';
    			$str[] = 'world';
    			$str[] = 'isdoomed</span>';
    			echo implode('', $str);
    			unset($str);
    		}
    		
    		$time_end = microtime(true);
    		$time = $time_end - $time_start;
    		echo $time, '<br>';
    	}
    ?>
    I surprises me that the first method is slightly faster.

    Does anyone have an explanation why?

    Greetings,
    x-taste

  9. #9
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Array is an internal class. When you use offset operators (the [$offset] syntax) you are invoking methods on the class. See ArrayAccess interface at php.net for details. You also invoked implode, another method, in the second example. The first method above directly concats a string and invokes no methods.

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clear reply!


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
  •