Hey all,
Attached is my first attempt at using OOP and MVC (well the best I understand so far). The files included display a very basic site with a blog, gallery and login form. I did not include the db.php file as it contains only private data that you are not allowed to know! I just want to know if I'm on the right track with how it is supposed to be set up.

I appreciate any comments you might have on my coding style as well, as long as you don't just say "well thats wrong" without saying why!

PS. Attached in zip cuz its a bunch of files.

    at first glance it does not follow the MVC but a good attempt to keep things separated i would suggest better code organization for readability and try to support a standard application design you seem to throw stuff in where you want not really logical structure.

      It also uses short tag syntax (something that would cause it to fail on every sever I manage) and global variables (making the use of OOP almost pointless, IMHO).

        What's not logical about it? Its logical to me, I've not worked with anyone else before or taken any type of class so I really need to know why its not logical. Could you provide any links talking about the application design you think I should use?

        No short tags, got it, will change that. Is <?php=$var?> valid? or do I have to do <?php echo $var; ?>

        If not to call $db and the table names by global how do I store all that in a single file and use those values in other classes??

          this here is a pretty good MVC tutorial the goal of MVC is to seperate your views(template's or template processors) and models(db , data , info that is not considered viewable) and your controller handle the models and views based on conditions and routing.

          some thing i sugggest are look into more advanced OO features of php such as spl_autoload_register() this is so you can make your own autoloader class and Exceptions for better error handling.

          i would also look at some design patterns as you will benefit from the logic behind them(will make you a better programmer).

          now as Brad said you need to kill globals and just call the class as needed this is where an autoloader comes in handy.

          and try to remember these rules keep SQL out of views and controllers keep your html/js seperate from your php(template engine) and manage your system object watchers and handlers(registry). also look at things like the factory and singleton patterns they are life savors.

            Just looking at the index.php file, the following comments (in PHPDoc-style "/**" format) may give you some ideas.

            While writing your own MVC framework can certainly be educational, there's a lot to be said, too, for learning by using an existing framework. The objective should be a structure similar to what you can see in the CodeIgniter video tutorials for separating responsibilities among the M, V, and C classes/objects as shown there. (Which is why I use CI when I want to use a MVC approach instead of reinventing the wheel, regardless of how interesting it might be to do so -- I have other things to do with my time. 🙂 )

            <?php
            /**
             * These things might better be handled in the controller >>>//
             */
            session_start(); //we must always start the session first
            include("./includes/db.php");  // Connect to database, set up table name variables
            /**
             * E.g.: what if a given page does not require database access?
             */
            
            /**
             * Again, some of this might belong in the controller. You *will* need some
             * code to decide *which* controller class/object to call, but then let that
             * controller figure everything else out (making use of class inheritence
             * for mutliple controllers)
             *
             * You might want to look into using $_SERVER['PATH_INFO'] so that you can have
             * URLs in the form: http://site.com/index.php/controller/method/arg1/arg2
             */
            // Get Common variables
            $action = isset($_GET['action']) ? $_GET['action'] : "";
            $curpage = isset($_GET['curpage']) ? $_GET['curpage'] : "1";
            
            /**
             * This stuff probably belongs in a view, which would then be called by the
             * controller
             */
            // Set-up default page settings
            include("./classes/page.php");
            $page = new page("Home Page");
            $page->addKeywords(array("my","home","page"));
            $page->addStylesheets("default");
            $page->setNav(array("Home" => "index.php",
                                "Gallery" => "index.php?action=gallery",
                                )
                          );
            $page->setTemplatePath("./templates/default/");
            $page->setCopyright(date("Y",time())." me and all my glory.");
            
            /**
             * This sounds like it's probably model stuff, which would then be called from
             * within a controller, not something to be here
             */
            // set-up user
            include("classes/user.php");
            $user = new user;
            
            /**
             * Again, this is some combination of view material plus logic that should be
             * in a controller class. Each case would instead be a controller method
             */
            // Determine content view
            switch ($action)
               {
               case "gallery";
                  include("./classes/gallery.php");
                  $gid = ISSET($_GET['gid']) ? $_GET['gid'] : 0;
                  $aid = ISSET($_GET['aid']) ? $_GET['aid'] : 0;
                  $view = new gallery;
                  $view->pickGallery($gid);
                  $view->pickAlbum($aid);
                  $view->setPage($curpage);
                  break;
               case "logout";
                  unset($_SESSION['user']);
                  session_destroy();
               default:
                  include("./classes/blog.php");
                  $view = new blog;
            }
            // Print the page
            $page->Display($view);
            

              Thank you guys for the suggestions, I guess I just dont get the C part of it, I understand seperating data fetching from data display but I guess I dont understand the middle ground. As far as using a premade MVC I think I'm going to go with CI of the ones I've looked at however, I have no projects and just wanted to see if I really understood it (which apparently I dont!) and try to understand it better.

              As far as URLs go NogDog I've been trying to learn using .htaccess but I dont quite get that heh. I guess I'm just more newb than I led myself to believe oh well lots more to learn!

                It's a continual learning process: you never know everything, partly because there is so much to learn and partly because so-called "best practices" evolve and mutate over time. (In a few years some new design pattern will be all the rage and MVC will be looked upon condescendingly as the old-fashioned way of doing things.)

                I think you'll find pretty much everyone here flailed around with OOP for some period of time until it "clicked". (Maybe a few exceptions for those really talented folks here with lots of formal training in the subject. 🙂 )

                  Hey, its gonna sound like im ragging on you but im not. This is exactly how I started out to a "T". You really just made a library of functions for your site. Its kind of like an API,,, Most of those methods you made do 1 thing and 1 thing only. Im not saying that you never going to create those because you will but the main goal of OOP is polymorphism. You want your class's to work together not be their own entities. You want your objects to be able to use parts of other classes. Your on the right track just keep learning and you will eventually get it.

                    This not a great analogy but the best I can think of. Think of a class as a VCR... What do you want that VCR to do... You want it to rewind,fast forward,stop and record. You made a VCR that can only record on a certain tape, channel 10 at 7PM on a Wednesday. Try to make them more dynamic to you can use that VCR to record on any tape, anytime and any place.

                      Vicodin that's a weird analogy, And I certainly didn't take it as ragging on me, especially the part about I'm on the right track. Also NogDog I finally ordered the PHP book you told me to get, should be here within a week, so expect better understanding soon hopefully =D

                      BTW what's a VCR? 😉

                        Haha, damn kids these days... Oh ya def stay away from those Globals, If I need something global I just put it in my DB and call it from there.

                          11 days later

                          Wow no one else has posted in this forum since this thread XD

                          Anyway I've downloaded CodeIgniter and tried to make a very basic user system hopefully in MVC but I'm sure there's some backwards things. See the attached zip.

                          Let me know If I'm getting closer to the right track.

                            Write a Reply...