SitePoint Sponsor

User Tag List

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 86

Thread: MVC in PHP

  1. #26
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    No, MVC is a pattern. Separating html from php is a implementation/interpretation of that pattern. It is very common to see that type of implementation in web based "MVC" inspired executions. However, MVC in itself does not dictate separation of html and php. That is a mere interpretation that began with the rise of RoR, cake, etc.

  2. #27
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    erm....OK. (Interesting Stuff!)
    How are views linked with the controllers?
    Does Cake have a form of Template Engine built into the framework?

  3. #28
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Within a MVC framework, how are views linked to the controllers?

  4. #29
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Within a MVC framework, how are views linked to the controllers?
    in php "usually" the controller interacts with the model and pushes data to the view

  5. #30
    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
    erm....OK. (Interesting Stuff!)
    How are views linked with the controllers?
    Does Cake have a form of Template Engine built into the framework?
    Cake was built on template engine :3
    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. #31
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I mean how is it done through code?

  7. #32
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's say you have a simple procedural model...

    PHP Code:
    // file: thing/lib.php
    function get_things_by_cat_id($id) {
        return 
    db()->query("SELECT name FROM things WHERE cat_id = $id");
    }

    function 
    update_thing($data) {
        
    extract($data);
        
    $stmt db()->prepare('UPDATE things SET name = ? WHERE id = ?');
        
    $stmt->bindValue(1$namePDO::PARAM_STR);
        
    $stmt->bindValue(2$idPDO::PARAM_INT);
        
    $stmt->execute();
        return 
    $stmt;

    And a file that looks like this...

    PHP Code:
    // file: thing/index.php
    $things get_things_by_cat_id((int)$_GET['id']);
    foreach (
    $things as $thing) {
        echo 
    $thing['name'] . '<br />';

    Is that a view or a controller? You might say view/controller combo, since there's a call to get_things_by_cat_id(), but traditional MVC allows read-only access to the model, so maybe not.

    The presence of $_GET, $_POST, etc., might tilt the scales towards a V/C combo, but those variables could also be interpreted as representing view state, in which case index.php is a "smart view" and the controller is the web server and PHP itself.

    Since there's a lot of ambiguity in this, many people simply call the V/C combo a presentation layer and leave it at that. You could obviously split that file into two files with more clearly defined V/C roles though, if you wanted to...

    PHP Code:
    // file: thing/index.php (CONTROLLER)
    $things get_things_by_cat_id((int)$_GET['id']);
    include 
    'html.php'
    PHP Code:
    // file: thing/html.php (VIEW)
    foreach ($things as $thing) {
        echo 
    $thing['name'] . '<br />';

    In PoEAA, Martin Fowler calls this type of view a Passive View. It doesn't make any calls to the model whatsoever. The controller is entirely responsible for fetching all data and making it available to the view. It can do this informally, as in the example above, or with a more formalized Presentation Model, i.e., a simple object that contains all of the information necessary to render the view, such as field names, disabled states, etc.

  8. #33
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Does MVC separate PHP code from HTML, or in other words processing from presentation?
    Where can I learn how to code a tailor-made MVC framework?
    It separates business logic from presentation to a large extent; but since PHP has an embeddable template syntax, it doesn't completely separate PHP from HTML (unless you create a new template language to go with it).

    You can learn by studying MVC frameworks that others have written. Retro is a pretty cool one and simple enough that you should be able to figure it out.

    I'm not sure building a "tailor-made" framework is the right reason to use design patterns. A major goal of patterns is to make code widely reuseable.

  9. #34
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting Stuff!
    Am I correct in saying the following?:

    • A "Framework" is a collection of "Classes"
    • A "Class" is a collection of "Functions"
    • A "Function" is a collection of "Raw PHP Code"

    Ordered Layout:

    Framework -> Classes -> Functions -> Raw PHP Code

  10. #35
    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)
    Nope.

    A class which is a collection of functions isn't (strictly speaking) a class.

    A class which is to be instantiated into an object should have properties, with methods referring to said properties.

    for example:
    PHP Code:
    <?php
    class Person{
        public 
    $Name$Age;
        function 
    __Construct($Name ''$Age 0){
            
    $this->Name $Name;
            
    $this->Age $Age;
        }
        function 
    Birthday(){
            
    $this->Age++;
        }
        function 
    __toString(){
            return 
    'I am ' $this->Name ', and I am ' $this->Age;
        }
    }
    $guy = new Person('Dave'32);
    $gal = new Person('Emma'40);
    echo 
    $guy;
    echo 
    '<br />';
    $guy->Birthday();
    echo 
    $guy;
    echo 
    '<hr />';
    echo 
    $gal;
    echo 
    '<br />';
    $gal->Birthday();
    echo 
    $gal;
    echo 
    '<hr />';
    echo 
    $guy;
    Whilst $guy and $girl follow the same blueprint (both are of the Person class, so have the properties Name and Age, and have the method Birthday), they hold different things.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #36
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you mean sort of ...
    So what would you say a true class is? (Does a class have to contain functions?)

  12. #37
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Interesting Stuff!
    Am I correct in saying the following?:

    • A "Framework" is a collection of "Classes"
    • A "Class" is a collection of "Functions"
    • A "Function" is a collection of "Raw PHP Code"

    Ordered Layout:

    Framework -> Classes -> Functions -> Raw PHP Code
    no a framework is a "framework" to help you develop applications quickly, its a set of classes, namespaces (in case of the current zend framework they have to compensate for lack of namespaces in php with Really_Retardingly_Long_Class_Names_That_Make_You_Want_To_SCREAM ), resources and tool to help you build applications rapidly

    it doesnt even have to be OO (.net is mix of OO and Event driven paradigm, god i sound like a snob saying that P word )

    ruby on rails is a framework, asp.net is a framework, zend framework is a framework (doh)

    also a collection of classes is a Namespace (coming too us with php 5.3, available in any other OO language since about forever)

    a class doesnt contain functions but methods and properties (i still remember my lecturer going mad when we students got mixed up and say "function" instead of "method" when it came to OO )

    so i hope i too can knock out the word "function" out of your mind and replace it with word "method" when it comes too OO and i hope i explained the difference between "framework" and "namespace"

  13. #38
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a class doesn't have to contain functions (aka methods), but in practice, most classes will have methods.

    Usually, your classes will be nouns (ie. Person, Car, Session, User) and your methods will be verbs or verbs + nouns (ie. getAge(), startEngine(), clear(), save()). As a good rule of thumb you should really consider your class as an object and ask yourself if that object (i real life) can perform the action that your defined methods perform. For example, say you're making a Person class. let's look at arkinstall's example:

    PHP Code:
    <?php

    class Person{

        public 
    $Name$Age;

        function 
    __Construct($Name ''$Age 0){

            
    $this->Name $Name;

            
    $this->Age $Age;

        }

        function 
    Birthday(){

            
    $this->Age++;

        }

        function 
    __toString(){

            return 
    'I am ' $this->Name ', and I am ' $this->Age;

        }

    }

    $guy = new Person('Dave'32);

    $gal = new Person('Emma'40);

    echo 
    $guy;

    echo 
    '<br />';

    $guy->Birthday();

    echo 
    $guy;

    echo 
    '<hr />';

    echo 
    $gal;

    echo 
    '<br />';

    $gal->Birthday();

    echo 
    $gal;

    echo 
    '<hr />';

    echo 
    $guy;
    some suitable methods for a Person might be getAge(), getGender(), or even getFriends(). Now...it sounds weird saying "Jake, get your age", but it does make sense if maybe you replace 'get' with 'tell' "Jake, tell your age". See what I mean? methods are verbs.

    Not to pick on arkinstall, but method names such as 'Birthday()' are not necessarily descriptive, and it's not obvious what happens without looking at the implementation. Also, in natural language... do people birthday? no...they HAVE birthday's, they can tell someone their birthday....but birthdaying isn't something i do often. So...a better name for this method might be haveBirthday().

    lastly, in case you were wondering, a Person would probably be the "Model" part of "Model, View, Controller"

    hope this helps you understand the distinction between a regular function and something that belongs in a class. to be honest, the best (and IMO, only) way to learn is by experience. people can teach you about Person's and Car's all day long, but until you make the connection to real world objects yourself, it probably won't do you much good.

  14. #39
    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)
    So...a better name for this method might be haveBirthday().
    So picky...

    True though.

    @OP: The best thing you can do in terms of OOP is find some online articles about it. My personal favorite has to be "Don't Fear the OOP", which is actually written for Java. Remember, though, that OOP patterns and terminology is pretty much consistent throughout languages, though the syntax may vary.

    It is based around an author who writes Western-style novels, and explains OOP VERY well, especially if you are a beginner to the idea.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  15. #40
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lastly, in case you were wondering, a Person would probably be the "Model" part of "Model, View, Controller"
    Is this because the persons details will be come from the database? i.e. Model being the database?

    OK. So I can get my head around this better, how would you code the following?:

    • User Registration
    • Account Activation (Email)
    • Password Recovery (Email)
    Last edited by F.Danials; Nov 21, 2008 at 19:22.

  16. #41
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    So what would you say a true class is?
    Essentially, a class is a template for creating objects. Unlike how many programmers seem to think, classes are not that important -- what is important are the actual objects, which are in practice (in PHP at least) instances of classes.

    Furthermore, an object is defined by its behavior, which is in turn defined by its externally accessible (public in PHP) members.

  17. #42
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Is this because the persons details will be come from the database? i.e. Model being the database?

    OK. So I can get my head around this better, how would you code the following?:

    • User Registration
    • Account Activation (Email)
    • Password Recovery (Email)
    Can anyone help?
    Last edited by F.Danials; Nov 21, 2008 at 19:22.

  18. #43
    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)
    What exactly are you asking?

    The theory for the above has nothing to do with application design.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  19. #44
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by F.Danials View Post
    Is this because the persons details will be come from the database? i.e. Model being the database?
    No, its because the person object "models" the real world.

    The model is much more than the database. The database is just data, but you also have behavior, or business logic, that acts on that data.

  20. #45
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    No, its because the person object "models" the real world.

    The model is much more than the database. The database is just data, but you also have behavior, or business logic, that acts on that data.
    spot on

    yes data from database is just a datastructure (row or rows ) it contains the data and nothing else

    for example Person would have following properties/attributes (age, eye_color, height, weight etc) these would be kept in a persons table in a DB (lets say a marketing company), its just data, nothing else, before OO (and in procedural php) this data would probably fetched as an array and passed onto a function to do something with this data

    Now lets say you have a Person class that you want to model on Person

    it would have properties such as age, height etc (listed earlier) but it would also have methods such as getAge(), isOverweight() and so on (note you only code in methods that are required for your model/specifications)


    so an object is DATA + FUNCTIONALITY in bundle

  21. #46
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    What exactly are you asking?
    In order to better understand what XtrEM3 said in Post #38 how would you code the following?:

    • User Registration
    • Account Activation (Email)
    • Password Recovery (Email)

  22. #47
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ionix5891 View Post
    so i hope i too can knock out the word "function" out of your mind and replace it with word "method" when it comes too OO
    Good luck with that. You use the "function" keyword to declare a method.

  23. #48
    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)
    Quote Originally Posted by F.Danials View Post
    In order to better understand what XtrEM3 said in Post #38 how would you code the following?:

    • User Registration
    • Account Activation (Email)
    • Password Recovery (Email)
    That kind of stuff won't help you understand the post at all.

    You can map a user to an object, simple enough:
    PHP Code:
    <?php
    class User{
        protected 
    $Username$Activated$Email$Password;
        function 
    makeActive(){
            
    $this->Activated true;
            
    save();
        }
        function 
    save(){
            
    // query
        
    }
        function 
    resetPassword(){
            
    $newPassword 'GenerateSomeLetters';
            if(
    mail($Email'Change of Password''You have a new password. It is '.$newPassword)){
                
    $this->Password md5($newPassword);
                
    save();
            }
        }
    }
    A lot more code to it but it's half 1 am

    Again, it's not the kind of thing you need to learn this.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  24. #49
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    That kind of stuff won't help you understand the post at all.

    You can map a user to an object, simple enough:
    PHP Code:
    <?php
    class User{
        protected 
    $Username$Activated$Email$Password;
        function 
    makeActive(){
            
    $this->Activated true;
            
    save();
        }
        function 
    save(){
            
    // query
        
    }
        function 
    resetPassword(){
            
    $newPassword 'GenerateSomeLetters';
            if(
    mail($Email'Change of Password''You have a new password. It is '.$newPassword)){
                
    $this->Password md5($newPassword);
                
    save();
            }
        }
    }
    A lot more code to it but it's half 1 am

    Again, it's not the kind of thing you need to learn this.
    OK I'll let you off. - I'm an +1 hours, I know the feeling (2:30am)
    Would it be possible to complete tomorrow? - It will help m get my head around things.

    So am I correct in saying that a framework is a collection of classes?

  25. #50
    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)
    Would it be possible to complete tomorrow?
    I'm not going to do your work for you. I have my own work to do at the moment and, on top of that, you need to be able to research these things on the net and come up with your own solution. 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.

    am I correct in saying that a framework is a collection of classes?
    Not really.

    Ok, so the framework can be made up of classes, but it can't be dismissed on such terms. A framework is a collection of classes, functions, plain code - anything really, that makes your site development faster.

    I'm an +1 hours, I know the feeling (2:30am)
    Now 2.30 here. I'm going to bed now - and I think you should do too if you haven't already.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •