SitePoint Sponsor

User Tag List

Page 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 86

Thread: MVC in PHP

  1. #51
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not going to do your work for you.
    I don't want you to. But, having something to base my code on, will assist/help me in the development of the other classes/functionalities.

    A framework is a collection of classes, functions, plain code - anything really, that makes your site development faster.
    But in my opinion classes would better though, as code can easily be re-used, saving processing power, and resulting in less loading time.

    But again, as I emphasised twice in my last post, your set of tasks isn't something you really build an object around, because they are tasks not oop related.
    So are you saying the "User" class isn't needed? - Why is this?

    Now 2.30 here. I'm going to bed now - and I think you should do too if you haven't already.
    I suppose I should , cheers for your help so far!
    Greatly appreciated!

  2. #52
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Frameworks simplify common tasks for the end user. That's really all a framework is. A Framework is not a class and a class is not a framework. A framework may be made up of several classes, but that is not what defines it as a framework. The classes are the mere medium of delivery or means to an end – nothing more.

  3. #53
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    I don't want you to. But, having something to base my code on, will assist/help me in the development of the other classes/functionalities.
    There are plenty of frameworks out there that you can look at as examples try looking them up.

    Quote Originally Posted by F.Danials View Post
    But in my opinion classes would better though, as code can easily be re-used, saving processing power, and resulting in less loading time.
    Procedural programing tends be be faster than classes in PHP and requires less processing.


    Quote Originally Posted by oddz
    Frameworks simplify common tasks for the end user
    I've never meet an end user that was smart enough to use a framework to develop something. Frameworks do simplify common tasks, but for developers.

    F.Danials, I would suggest not attempting to create your own framework, or at least not yet. There are plenty of frameworks out there that would definitely serve your purposes while teaching you several programming practices that would help you grow as a programmer. The best way to grow as a programmer, is to read and apply "good" code. I developed my own framework after 2 years of learning PHP. The result was a hodge-podge of bloated code that got the job done but was hard to read, maintain and poorly commented.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  4. #54
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    The end user being the target for the framework. In many cases those targets are other developers.

  5. #55
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understood what you were attempting to say. However, end user refer to those who execute an application and typically the term is associated with non-programmers.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  6. #56
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Under most circumstances frameworks directly support the developer intention not those viewing a site built on a framework – they could probably care less. So the target of the product is essentially the developer who chooses to implement the framework to achieve their goal.

  7. #57
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then you should probably say developer or programmer rather than end user.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  8. #58
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, when you are building a framework, the developer IS the end-user (because its for them). Anyway, we know that he meant programmer so let's leave it at that .

    F.Danials, whilst a framework does make your app slower, it's well worth it.

    For example, in procedural code:
    PHP Code:
    <?php
    //verify login:
    $username mysql_real_escape_string($_POST['username']);
    $password mysql_real_escape_string($_POST['password']);
    $query mysql_query("SELECT id FROM users WHERE username = '{$username}' AND password = MD5('{$password}')");
    if(
    mysql_num_rows($query) > 0){
        
    $_SESSION['user']['id'] = mysql_result($query0);
    }
    is faster than:
    PHP Code:
    <?php
    $user 
    User::CheckLogin($username$password);
    if(
    $user !== false){
        
    $_SESSION['user'] = $user;
    }
    But the second is most certainly easier for the developer.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #59
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dunno if he wants to develop a framework, why not

    i learned alot while making one for myself and currently use it to great effect serving millions of people

    they say best way of learning is getting your hands dirty


    btw F.Danials, i highly recommend sitepoint's own PHP Anthology book
    http://www.sitepoint.com/books/phpant1/

    read it few years back and i can say its well well writen

  10. #60
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I second that.

    It'd also be a good learning excersize to build a framework. You'll find yourself remaking it over and over again, and learning something new every time. The key step to writing a framework is to focus entirely on the guy using the framework first.

    Make it as easy and straightforward as you can for him, and fill in the framework based on that.

    For example, I'm currently building a framework for a series of sites. When making the templating system, I thought 'what would be the best way of inserting data?' and I first came up with an XML-based approach, a bit like ASP.NET, where the items with the 'insert=...' tag would have that code parsed then inserted into the tag. However, this really didn't have much benefit, and it would require a learning curve to use it, so I came up with this:
    Views/Topic.tpl
    HTML Code:
    <h3>~Name~</h3>
    <ul>
    #LOAD EACH GetArticles INTO InfoBox_Article#
    </ul>
    Views/InfoBox_Article.tpl:
    HTML Code:
    <li>
        <h4>~Title~</h4>
        <p>~Description~</p>
        <p>#ShowRating#</p>
    </li>
    The # shows an object method, the ~ shows an object property. The #LOAD.... loads each element returned by the function call (GetArticles()) and outputs each in the template file InfoBox_Article.php.

    When you know the desired abilities of the framework, you can THEN start on building it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #61
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes doing some .net is quite an experience as you can borrow alot of ideas and port them to php

    you have to give it to microsoft .net and visual studio is quite an environment conductive to high productivity

    manys of time do i curse at zend studio for being very lacking and slow

  12. #62
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Meh, I do my apps in NotePad++

    I used to use PHPDesigner, but I didn't bother continueing when asked to pay for the license because, although the code completion is handy, I am perfectly comfortable with the free NotePad++.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #63
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    VI and Vim FTW
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  14. #64
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I have the feeling this is going to turn into one of those IDE convos. Let's not go down that route.

    F.Danials - are you considering going through the framework route?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  15. #65
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Blimey that's better - A good long sleep followed by a nice cup of coffee really hits the spot

    are you considering going through the framework route?
    Yes, as it will assist me in carrying out maintenance, additions, and deletions.

    I'm aware that using a framework can slow down the whole system, but for this overhead, you do receive quite a lot as a reward. There wouldn't really be any noticeable speed difference, would there?

    How come you used the '#' and '~' characters for inserting data into you templates, don't many systems use '{}'?

    I still can't get my head around how the template files (Views) containing the '#', '~', or '{}' characters get linked to the processing files (Controllers). Would it be done using regular expressions ?

    How would you create a simple application that displays Hello World in the title bar of the template the will probably look like the following, how what would you add to the "Controller"?

    Template Example:

    Code:
    <html>
    <head>
    <title>{title}</title>
    </head>
    
    <body>
    </body>
    </html>

  16. #66
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, it all depends on how you WANT to do it.

    First of all I'd make a View class:
    PHP Code:
    class View{
        public 
    $Object$File;
        function 
    getVariable($matches){
            
    $match $matches[1];
            return isset(
    $this->Object->$match) ? $this->Object->$match '';
        }
        function 
    __toString(){
            
    $Content file_get_contents('Views/'.$this->File.'.tpl');
            
    $Content preg_replace_callback('/{([A-Za-z\s\d_\-]+)}/', array($this'getVariable'), $Content);
            return 
    $Content;
        }
    }
    $View = new View;
    $View->Object = new Article('SomeRandomTitle!');
    $View->File 'Main';
    echo 
    $View
    Views/Main.tpl:
    HTML Code:
    <html>
    <head>
    <title>{title}</title>
    </head>
    
    <body>
    </body>
    </html>

    How come you used the '#' and '~' characters for inserting data into you templates, don't many systems use '{}'?
    I use those characters because they have separate meanings. The # means a function call, and the ~ means a variable, i.e:
    HTML Code:
    <p>My name is ~Name~, I am #CalculateAge#</p>
    Plus the { and } is a bit to clich&#233; for my liking, and when going through code, I find ~hello~ easier to spot than {hello} because of the protrusions on the ~.

    There's method to my madness...
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  17. #67
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How the hell do you learn all this stuff? - Can you recommend any books/websites etc?
    This style of coding is right up my street! (Just what I need.)

    So I can see how it works, I attempted to execute the above code, but I received the following error. Why is this?

    Fatal error: Class 'Article' not found in c:\apache\htdocs\class.php on line 28
    Am I correct in saying that there is little if not any noticeable difference in speed between using a Framework/OOP and Procedural code?

    PHP Code:
    <?php
    class View{

        public 
    $Object$File;

        function 
    getVariable($matches){

            
    $match $matches[1];

            return isset(
    $this->Object->$match) ? $this->Object->$match '';

        }

        function 
    __toString(){

            
    $Content file_get_contents('Views/'.$this->File.'.tpl');

            
    $Content preg_replace_callback('/{([A-Za-z\s\d_\-]+)}/', array($this'getVariable'), $Content);

            return 
    $Content;

        }

    }

    $View = new View;

    $View->Object = new Article('SomeRandomTitle!');

    $View->File 'Main';

    echo 
    $View;

    ?>

  18. #68
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    That would be because the class was not defined in the example. The error says it all: Class 'Article' not found.

  19. #69
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah! Well spotted. I missed that
    What would the "Article" class consist of?

  20. #70
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just a public property $title, and a construct method which sets the passed value to that.

    How the hell do you learn all this stuff?
    Theres a great site which I've learned almost everything I know about PHP on. You're looking at it
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  21. #71
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Theres a great site which I've learned almost everything I know about PHP on. You're looking at it
    haha you should be seeing some free books coming your way for that plug

  22. #72
    SitePoint Enthusiast snajt's Avatar
    Join Date
    May 2008
    Location
    Kalmar, Sweden
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While we're talking about templating, how would you make use of the composite view pattern? I've done some searching about this but could not come up with a solution.

  23. #73
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by snajt View Post
    While we're talking about templating, how would you make use of the composite view pattern? I've done some searching about this but could not come up with a solution.

    my views allows for master templates, main view file + "codebehind" and several components

    heres example from page controller i was working on this morning, and example how the view is created and parameters passed in

    PHP Code:

            
    //create item object
            
    $Item = new Item
                new 
    ItemDatabase$this->Registry->DB ),
                
    $this->Registry->Settings->items
            
    );
            
            
            
            
            
    //fetch the hotlist
            
    try {
                
                
    $hotlist     =    $Item->fetchHotlist$this->Registry->Session->getUserId() );
            }
            catch ( 
    ItemException $e){
                
                throw new 
    Exception$e->getMessage(), 500 );
            }
            
            
            
            
    //fetch the asset summary
            
    try {
                
                
    $register_summary     =    $Item->fetchSummary(  );
            }
            catch ( 
    ItemException $e){
                
                throw new 
    Exception$e->getMessage(), 500 );
            }
            
            
            
            
    //create a view
            
    try{
                
                
    $View = new View
                    
    $this->Registry,
                    
    'normal'//normal master template
                    
    'default__admin'//view file
                    
    array( //components
                        
    'default',
                        
    'item'
                    
    )
                );
                
                
    $View->setParams$this->Registry->CommonView );
                
                
    $output    =    $View->render(array(
                    
    'hotlist'            =>    $hotlist,
                    
    'register_summary'    =>    $register_summary,
                ));
            }
            catch( 
    Exception $e ){
                
                throw new 
    Exception$e->getMessage() );
            }
            
            
            
            
    //output
            
    $this->Registry->HttpResponse->appendNoCacheHeaders();
            
    $this->Registry->HttpResponse->appendHeader('Content-Type: text/html; charset=utf-8');
            
    $this->Registry->HttpResponse->setMessage$output );
            
    $this->Registry->HttpResponse->output(); 

  24. #74
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ionix5891 << Why is your HttpResponse a child node of the Registry?

  25. #75
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so any post processing filters could be easily added as flow of control leaves the page controller snip above

    oh and for testing purposes, can examine the registry and the results, since everything important is in one place its easier to run tests, not to mention not all output is sent as http response other, i have alot of cli output and few socket based deamons etc

    the registry lets me avoid globals and helps simplify testing, which is well why people use them

    tho i have to admit i hate the
    Code:
    Lack->Of->Proper->Autocomplete
    in all PHP IDE's i tested


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
  •