Jump to content

Recommended Posts

I'm trying to move my site over to using classes and just need a little bit of advice on how to structure it. I've done research on classes and singletons and the functions within classes and such.

 

Here are the basic functions of my website:

  • Login & Registration system with users stored in a database
  • Reviews/Interviews/News items stored in a database
  • A staff section where items can be edited/created and deleted
  • A usersonline table that tracks the users that are currently online (i want a class that displays this number - i think it'd be a singleton class but i'm not sure what it should extend and such)

How exactly would I structure the classes for this sort of system? As in, what would be parent classes and what classes would extend the parents and such. I'm having trouble figuring out how it should be structured.

 

I promise that I have done the research and I'm not just asking anybody to do it for me. Any help is appreciated :)

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/
Share on other sites

Moving your site into a random collection of random classes would be unwise and not very productive.  Classes are good...but they need to be used in the right way(s) in order to help more than hurt.

I would create 1 class for each "type" of page structure you need.  Note to mention you would need a dispatcher class and a front controller.  If you don't have a framework you can use one of the existing frameworks out there that have gotten a great deal of attention.  Code igniter, cakephp are 2 of the biggest out currently.  There are others but those will give you something to look at.  But as far as "Classes" (not including the front controller and routers and whatever classes you'll need to call the pages as needed) I general prefer a grouping approach.

My user's class wuold contain all functions related to registration, login, logout, user security, forgot your password.  This would handle all actions related to "Users". Including showing/editing there profiles.

 

Then I would have a second class to handle my database "Items".  Reviews/interviews/news.  IF one of those items are going to be used for many different things I might split them up into seperate classes to keep it neat (one for reviews, one for interviews, and one for news).

 

For the staff just create a staff's class that will handle everything for the staff (there login, logout, edit, view profiles) and everyting else.  You need one to handle there "Admin" functions together or put them all together under the same admin class.

The table could be brought about using a function in the users class.

You would need a front end class seperate that is going to "Recieve" the incoming url/post methods and help decide what pages are going to be shown.  I would recommend a class seperate that can handle loading the config options (if you have any)  You are also going to probably have helpers, and js files, and stuff that you will need to developer a "Loader" for.

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/#findComment-573584
Share on other sites

What exactly are these front end classes you're talking about? (sorry, i'm new to classes!)

 

The way I'm currently loading pages is simply with html and php. Can this also be done with classes? If so - what are the benefits of that and is it too difficult for someone relatively new to classes?

 

 

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/#findComment-573637
Share on other sites

A Front Controller is just that.  It's what intercepts information about what the person is trying to see...and directs the programming to find it.

If you have index.php with a class inside of it which is your front controller.  Thetn what happens is when someone is visiting that page it's generally setup with a function/parameter (parameter being the page that needs to be found) then a simple switch statement, or logical control structure can then tell PHP which class we need to go get for that page.  Then from there it can call out the index function to gather what is needed and display the first page.  THen in the future they can append new things onto the class which will hit the front control and add that into the page that's being called (which will call that class + the function name provided + the parameters.  Like CakePHP.

You have

www.sitename.com/controllername/functionname/param1/param2

Something like that.

www.sitename.com tells the browser where we are going to look for the site (the server).  Then it finds it and sends out the commands Controllername...which is the name of the controller.  This is grabbed by the front controller and that's what tells the front controller which class to call.  Then the next one is used which is the functionname...this is the function that is called within the class.  Param 1 and 2 would be parameters of that function.

 

Say you have a users class.

Your first function is your starting function index() and it's going to be called if no function names are provided via URL.  Then if you wanted something for users to logout it's

www.sitename.com/users/logout

Which will access the users class and logout function which will perform the code to log the person out.  Then same with login.  YOu would have

www.sitename.com/users/login/intensity

Say for that example if intensity is 1 then it's a persistent login..it ifs 2 then it's just until the browser closes.  Then the username/password would obviously be sent via post.  Just a quick example.  A front controller simply controls where the user is sent.  Based on what URL they are visiting.

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/#findComment-573747
Share on other sites

If you don't want to mess with MVC just yet, remember that classes should have a single responsability to them. A class that takes care of Users and Permissions is doing too much, break it up in two classes instead. Classes should (it's not always entierly possible, though) be able to function alone without the need for others, yet also be able to work together with the rest of them in your application. One of the real difficulties in OOP is achieving just that.

 

And yes, everything you can do with common procedural PHP you can do with classes and object oriented approach. The trick is getting it right; It might take a while to grasp the more complex and useful underlying imlpications of OOP.

 

If you are just starting out with classes and OOP, try something small. Decide on ONE thing to port from procedural to OO and do it, then, try implementing it in your site. It will give you a nice real contrast between the object approach and your former procedural. When you are comfortable with that one object, decide on another one and do it. Think of how your new object will interact not only with the procedural code now, but also with your first object... do they have to comunicate? if so, how? do they share common functionality? perhaps they have overlaping responsabilities... refactor them or create a third object that takes care of that common routines. How will this third object "talk" with the other two now?

 

You will probably stumble across very common problems, to which solutions already exist in the form of "Design Patterns". For example, Registry and Dependency Injection are two patterns that attempt to solve the object comunication issue.

Front Controller and Page/Action controllers fall in the category that businessman mentioned as "front end", they decide which parts of your site to load an display when visiting different pages.

 

And finally, remember that although MVC is very popular and very useful for web applications (Here is a nice tutorial: click me gently ) it is not the only way to build pages. And it's probably a good idea to get some OOP excercise and grasp of concepts before trying to develop a full scale MVC site.

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/#findComment-573761
Share on other sites

Thanks everyone for your help. So I downloaded cakephp and i'm going through the blog tutorial now, it seems like something that is acheivable and would definitely help a lot in the long term approach.

 

Would you guys recommend cake as something that I should use and that could fulfill the needs of my site? And also, would this be open to future expansion - such as integrating the login system with one of a forum software (i plan on doing this in the future).

 

If it wasn't too much trouble, I was wondering if you could give me a brief rundown on the layout of this MCV stuff. I've read the stuff about it but just need some clarification on a few things.

 

  • The extensions used are .ctp. I'm used to simply using .php, what is this extension and why is it used instead? what's the difference between it and php?
  • for adding a post in the blog tutorial i'm using, this is the code that is used:
    echo $form->create('Post');
    echo $form->input('title');
    echo $form->input('body', array('rows' => '3'));
    echo $form->end('Save Post');
     
    whilst i understand this, sort of, i don't understand how it is adding information to the database in the background. I haven't given it a table or anything, and it still inserts it. why is this? what is going on behind the scenes that I don't know?
     
  • when making new pages (or views, or whatever), it applies styles and it has the cake header and a table showing the queries at the bottom. why does this happen? I haven't included any functions or includes on the page? where is it getting this data from, and how can i change it?

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/#findComment-573765
Share on other sites

Cake or codeignitor will do everything you need. Also once you learn more you can adjust it to meet your needs and carry your modified version around with you.  As far as which one I recommend...neither directly.  I have tried them both and like them both.  If you like the hands on approach then Cakephp.  but it's also harder to getting running on some servers.  www.1and1.com for example (very hard to make cake work there) but it can happen. Overall IN MY OPINION i think that CakePHP is a better more refined framework than code ignitor...but I think codeigniter is easier/faster to get setup/started when it's a small project and/or your in a hurry to get something rolled out fast.  If you have more time during prep then Cakephp is what I recommend but sometimes time is not an option we have..

 

File extensions can be what you want them to be.  There naming conventions.  Views are generally .php, .ctp, .tpl, or .thtml.  Pick which one you like and you can alter cake/code igniter to use those extensions for views.  Very simple to setup initially.

 

The idea behind it is it's getting the data from the model automatically.  YOu need to study there API and docs about there Model setup and how the Controllers relate to the models and they both relate the the view.  Basically controller is what is first called it grabs stuff from the Model and puts it together in the controller and passes it into the view.  The Model is what you do all of your MYSQL inside of.  You still have to write/create the queries (generally using cake builtin methods if you choose) but it just keeps them seperate and makes it faster.

 

It automatically filters them in using the templating model they have.  Look for there templating folder and you'll find the header/body of cakes default format.  You alter that to the style of your "site" and after that all new pages you create will automaticaly take on the layout without you having to copy/paste layout code for each and every page you try to do.  Do some google searches on cakes built in Templating system and you should find everything you need.

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/#findComment-573835
Share on other sites

Thanks for your help. So I have no idea where the default template they are using for their pages is so that I can try and study it and figure out how they work it.

 

After it mentioning it on the first page, I went ahead and created the "apps/views/layouts/default.ctp" file (i thought there would already be one there that they were using but there wasnt). This just has a blank page and renders as standard html. Now I can make the headers and everything using this page, I understand. But how do I tell it to put the content in the middle? If you understand what I'm saying?

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/#findComment-573851
Share on other sites

You misunderstood.  Cake handles ALL of that automatically.  The coming out of the view is passed into a string...that string is echo'd out in the template file and affectively shown in the page.

I am also not sure what version of cake you are using.  I have only temporarily messed around with the newest version.  In the older version it was

/app/views/layouts/default.tpl

That is where I generally set mine up at.

 

Now before you do anything else..stop.

Go to http://book.cakephp.org/ and read EVERYTHING there.  Take notes...read it and re-read it.  That'll teach you EVERYTHING you need to know about setting up a basic Cake structure...how the controllers/models/views relate.  And everthing about how to setup a bare bones basic app.

After you get that under your belt then it's time to take a look at api.cakephp.org for a detailed listing of all there objects/methods for both the models, controllers, views, and speciality classes.

Link to comment
https://forums.phpfreaks.com/topic/111647-class-structure/#findComment-573865
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.