Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am curious as I am currently using functions exclusively in my webpages. The MVC pattern is very interesting, and I know Code Igniter utilizes classes which works very well. I want to be able to keep my code as clean as possible, and I thought about trying to move my functions into classes.

Currently I am separating the files by logic, so I have functions that output the html with arguments that pass any dynamic content, and the functions which handle the user input. I also have functions that interact with the database. I require and include the necessary files between them. As one can tell, there are a lot of arguments being passed around. So that leads me to wanting to try a style of OOP in PHP that I can do the same without having any html inside of the actual class.

I really don't want to hear anything about frameworks as the point is to learn and incorporate these ideas into my own website. That being said, I am fairly new at web development, so I do not understand the many different styles in setting up the logic of websites.

I would like to get some insight on how to best clean up my code in OOP?

EDIT: It is fairly obvious people have strong feelings about frameworks. So I decided to make this very clear; MY WEBSITE IS A PROJECT FOR LEARNING. I WISH ONLY TO LEARN FROM IT. THEREFORE I do not wish to use a framework as it makes more sense for me to learn how it all works before making my life easier and actually use a framework. A FRAMEWORK FOR MORE ROBUSTS PROJECTS. Thank you.

share|improve this question

closed as not a real question by MainMa, gnat, Walter, Jarrod Roberson, ChrisF May 1 '12 at 15:06

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
It seems to me that you are learning and looking for a mvc architecture at the same time. Mvc will clean up your code as it will dicide them into modules - models for database structures and relations, view for anything related to ui/html etc and the controller for the actual logic part. –  Bibhas Apr 28 '12 at 2:38
3  
functional programming is different than what you're talking about. –  timpone Apr 28 '12 at 3:16
    
To clarify @timpone's point, functional programming generally refers to the paradigm that languages like Haskell use. I think you mean to say imperative programming. –  Maxpm Apr 28 '12 at 4:24
    
I apologize for not being so clear. But I am still currently learning, and my approach is definitely un-cluttering my code, but I just want to see what more experienced developers have to say about how they design the backend without an actual framework. –  Andy Apr 28 '12 at 6:11
    
"I really don't want to hear anything about frameworks, as the point is to learn ..." is like taking music lessons and saying, "I really don't want to hear anything about scales, modes and chords as the point is to learn ..." –  Jarrod Roberson Apr 30 '12 at 18:30

5 Answers 5

PHP & OOP

I will directly quote Wikipedia as I feel that their articles are clearer and quicker than I could ever be.

Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs.

What you are currently doing could best be defined as imperative programming.

Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way that imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform.

PHP facilitates both but OOP is slowly becoming THE way to develop "good" applications.

Imagine having the choice between doing something the way that is easiest or the way that is best long-term ... most obviously lean towards what is easiest but understand the benefits of the other. OOP will allow you to understand software development better as well as making you applications more maintainable and logical but it will require an investment of time and practice. It is not something you will learn in one night but you will be glad to have it on your tool belt. I could even go out on a limb and say you may even find yourself being more efficient and developing better applications with it.

MVC

Trygve Reenskaug formulated the model-view-controller (MVC) pattern about as follows.

In software engineering, Model–View–Controller (MVC) is an architectural pattern that splits interactions between users and applications into three roles: the Model (business logic), the View (user interface), and the Controller (I/O).This separation of concerns facilitates the independent development, testing, and maintenance of each role.

Not using frameworks would hurt you in understanding MVC and in understanding its proper implementation. Now that is a double edged sword because modern frameworks aren't technically "MVC Frameworks" as they all interpret the pattern in their own way. If I were you, I would take a few popular frameworks (CakePHP, Yii, Symfony, Zend Framework, etc.) and take the time to try each and find the way that YOU like best and eventually learn to implement its way into a custom framework down the line. Learning MVC without a framework is possible but much more work.

share|improve this answer
2  
who is "Trygve" and whose "baby" are you talking about? –  gnat Apr 30 '12 at 17:27
    
"Object-oriented programming has roots that can be traced to the 1960s", I dare say it has been THE way to develop good applications for a few decades now. –  Jarrod Roberson Apr 30 '12 at 18:33
    
"Trygve" is the "creator" of the MVC paradigm and @JarrodRoberson very true, but the PHP world isn't the same world you and I know. –  Jacob Krustchinsky Apr 30 '12 at 20:26

First off what you are talking about isn't functional programming, it is more accurately imperative programming which has been proven not to scale to massive lines of code because of the lack of organization structures.

Object Oriented programming is a direct response to the mess that imperative programs had begun to become because of the in ability to maintain massive lines of code in the imperative style.

From your comment

but I just want to see what more experienced developers have to say about how they design the backend without an actual framework.

experienced programmers don't design backends without frameworks because they know from experience it is a waste of their time, they end up writing their own framework instead.

Unless you are in the business of writing frameworks, writing your own is not producing anything in your problem domain, and keeps you fixing bugs and updating your hacky not a framework framework rather than spending valuable time actually solving your domain specific problems.

For learning

Every popular framework for PHP is Open Source, start reading the Source of a few popular ones. They are popular because they work are best practices as far as PHP is concerned. This is how every programmer learns and understands what has come before them and how to leverage other more experienced peoples knowledge.

share|improve this answer
    
I've learned from experience that when asking a question on programming all I get is use a framework. In essence, very few people know how to code a website without it. Very few people learn the underlying code behind a framework. I Acknowledge its uses, and so I would like to understand the code behind it, hence me explaining that above. I do apologize for the "imperative programming" mistake, but the whole point is to learn. To get ridicule for doing so impedes it rather that fostering it. –  Andy Apr 30 '12 at 18:19
    
So I ask you. Please explain to me how YOU would answer my question, which is how to turn my approach and make it MVC like. What I am asking is to not say USE A FRAMEWORK and ask that you explain how it can be done without quoting a framework. I understand OOP, and so I would like advice on how to structure it in that manner to clean up code. I hope that was clear enough. –  Andy Apr 30 '12 at 18:22
    
Asking for help and then telling everyone what you don't want to hear about isn't how to ask a question and get the best most appropriate answers from those more experienced than you. –  Jarrod Roberson Apr 30 '12 at 18:24
1  
-1: Reinventing the wheel is perfectly fine for personal education! –  Maxpm Apr 30 '12 at 20:00
    
Thank you for that @Maxpm Thats exactly the point! –  Andy Apr 30 '12 at 20:54

Excuse me if I'm a bit off base here, but it sounds like what you really seem to be asking for is information about design patterns.

MVC, MVVM, MVP etc. are all design patterns for decoupling views from operations and data. That is, they help you structure your projects such a way that things make more sense, data flows in the correct directions, and you can potentially replace certain elements/layers without having to break the others.

If that's the case, 3 of the better known books are:

None of which use PHP, but design patterns are generally language agnostic.

share|improve this answer
    
Well I was not asking for an actual design pattern, but I do appreciate the sources you have provided as they are helpful. Thank you. –  Andy Apr 30 '12 at 20:15

The basic structure for a MVC homemade framework would be like :

  • Grab the request arrays ($_GET, $_POST, $_SESSION, $_COOKIE);
  • Prepare a new response object
  • Pass both to a front controller
    • The front controller job is to analyse your request and instantiate the right class to do the work (AKA the CONTROLLER (and probably the right method (AKA action)) and pass the request and response object
  • Based ont the request params, in you action you call the required MODELs to do the heavy lifting, connect to the database, parse data, etc.
  • After all the processing that had to occur, occured, you send all the processed data to an other class (AKA the VIEW) which will then process all the received data and store it in the response object as HTML.
  • Return the response object and print it

That is the most basic MVC stack you could get to make sure you have separated your application by function.

Over that you could add a lot of stuff, classes for managing your database, your tables, your session, your cookies, your forms, your form validation, etc. In the end, that's why everybody will eventually tel you to use an existing framework, it can easily become a lot to maintain with almost no benefits since you won't have access to specific support and you might end up refactoring your framework a lot and thus, breaking backward compatibility between your different applications

share|improve this answer
    
Ahh, yes I understand. The website I am doing isn't anything that has sensitive information, and its a fun project that could potentially be something, but I am purposely doing things from scratch as to learn, which is why I am avoiding a framework. But I completely understand. Questions though, so I should have an object that itself passes or is passed to other objects like the controller, views, etc? And thank you, I appreciate you answering my question. –  Andy Apr 30 '12 at 20:27
    
You want to have your structure loosely coupled, so if you pass objects around instead of having them instanciated in the constructor, you can replace them more easily (ie: you rework your response object to include a templating engine, then you just have to pass the new version and voilà, it works, instead of doing a lot of find and replace) –  JF Dion Apr 30 '12 at 21:25

You should definitely not use php functions that output HTML, that code is very hard to maintain and understand. Instead, make a folder called 'views' or 'templates' or just 'html', and put all your html files inside.

For example, if you have a signup form, put its html in views/signup_form.php, then inside your main signup.php (which contains only the php code for handling the sign up process), you can just do include views/signup_form.php when you want your html to be displayed.

You mentioned that you have to pass on a lot of arguments to your functions. A cool way to overcome that by using OOP, is to do this:

class Person
{
    //Arguments that you need to pass:
    private $name, $age, $gender;

    function setName($value)
    {
       $this->name = $value;
       return $this;
    }

    function setAge($value)
    {
       $this->age = $value;
       return $this;
    }


    function setGender($value)
    {
       $this->gender = $value;
       return $this;
    }
}

Then, to set all 3 arguments, you could do this:

$bob = new Person();   
$bob->setName( 'Bob' )->setAge( 30 )
       ->setGender( 'male' );

Hope this helps

share|improve this answer
    
Thank you very much. Finally someone actually answering the question. One question, when including the views/signup_form.php, how will it have access to information previously inputted if they failed to provide all necessary inputs, for example, a sticky form? This is what I have been having trouble understanding how it could make it easier and cleaner to use. –  Andy Apr 30 '12 at 20:22
    
For example (I just thought about it), if I want to add dynamic info on my signup_form.php to put some info the user previously inputted so as to make their life easier. If I just include a raw html form, I wouldn't have the dynamic content I need. –  Andy Apr 30 '12 at 20:51
1  
@Andy this is where a framework such as codeigniter becomes very handy, it takes care of things like form validation, passing dynamic values to your html templates, etc. Here are some links that answer your question directly with how to do these things in codeigniter, you can replicate the same method in your own projects even if you dont use codeigniter. codeigniter.com/user_guide/general/views.html codeigniter.com/user_guide/libraries/form_validation.html –  Click Upvote Apr 30 '12 at 21:02
    
thank you. Though I have tried that, and their is a lot of convention that is confusing. For example, putting a html inside a file called blogview.php In the example thats all thats in the file. When calling it it uses $this->load->view('name'); But where is that located. How does it even have access to it through $this. These are the parts that confuse me which is why I have decided to learn ways to do it on my own and then dive into a framework when I have. But I appreciate the advice. –  Andy Apr 30 '12 at 21:16
1  
@Andy, that's where you need to start looking at the source files of the framework to understand how they do it. –  Adrian Schneider Apr 30 '12 at 21:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.