MyBB Community Forums

Full Version: Getting into OOP, the MVC pattern and frameworks
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
I'm done with procedural PHP, I want to learn OOP. I have a very basic understanding of what it is and how it works, but I'm still very clueless about it. All I've done was use PDO, which is awesome by the way. I already have a couple of tutorials bookmarked and some videos I plan on watching, but feel free to link me to more stuff.

In addition to that, I also want to learn about the MVC pattern. Basically, all I know is it's a way to logically separate your code into model, view (what's seen by the user) and controller. How it actually works? I'm not sure yet. In order to learn all of this I was thinking of using a framework, but I'm not sure which. So what would be a good one? Bare in mind I'm still a beginner, so I'm looking for something nice and lightweight that I can learn from - not the best framework overall.

I've been looking into CodeIgniter, Kohana, CakePHP, Yii and even the most recent one, FuelPHP. But I just don't know which to pick. If I had to make a choice though, I'd go with CodeIgniter because of the large community and great documentation - perfect for a beginner. However, I want to hear from you. What framework would you recommend to a noob like me?

Thanks!
Probably Yii so you can get used to how MyBB 2.0 will work Toungue
I've had experience with CI which is really good for beginners. If you have a relatively good knowledge of PHP then you could also dive in to Zend Framework (I'm in the process of writing a piece of software in ZF was we speak) which although isn't very lightweight it's packed with everything you could ever need.

It's really about what you feel comfortable using. Smile
If you want to lean OOP, buy a book. That's honestly my best advice. I learned much more by reading a good book than I would have just reading tutorials online. As for choosing a framework, I'd also advocate looking at Yii - not just because MyBB's using it, but because it has some pretty great documentation and it's fairly simple compared to CI.
OOP is very easy (for me), I'm done with procedural too, it (OOP) makes it really easy to do every thing, update script/delete some parts....

All you have to do is to think of your script as "Objects", what objects will you need to build your script?? etc...

And then you have classes that define those objects, for example:
class Post
{

}

now you can create a new "Post" object easily by doing
$post = new Post();

There's also something called "the constructor, what do you need to do when you create your object??

Let's say, I want a class that displays a given name,
I will need an internal variable called "$name", so whenever someone creates a "new" object of my class, he will have to give me his name...


class MyName
{
//the internal variable is private
private $name;

//the constructor, this is how we build a new object
function __construct($name)
{
//the user gave me a "$name", all I have to do is to associate it
//to the internal private variable above :
$this->name = $name;
}

//a function to display the name :
public function displayName()
{
//returning the internal variable, as easy as that :
return $this->name;
}
}

/* now let's create a new MyName object (remember we have to to give a "$name" to the constructor)
and then let's display it
*/

$theGarfield = new MyName("TheGarfield");
//object created, with the name "TheGarfield", now let's display it using the other function (called method in OOP)

echo $theGarfield->displayName();

Try this and see the results Wink
If you understand the code above than that's a good beginning Wink
Look at the MyBB DB classes to see how they work. That's what I did.
OOP may look like a nasty mofo but I promise it's not. Smile It gets very easy very fast once you get your head around the initial concept.
We use Kohana (2) at work and it's very easy to use.

Once you go black OOP, you never go back.

Also worth noting though that the MyBB uses of classes isn't a good example of OOP, because it's not really OOP. It doesn't work the same as using an OOP framework.
OOP is great because it lets you keep things logically separated. If changing a class variable behind the class's back would break things, make it private. Don't pathologically use getters and setters (i.e. getFoo() { return $this->foo; } and setFoo($newFoo) { $this->foo = $newFoo; }) if all you do is assign and return, just make it public. It's much faster to just access the variable directly. If you need to do things like validate (i.e. setFoo($newFoo) { if($newFoo > 100) return; $this->foo = $newFoo; }) then you should go ahead and use getters and setters and private variables.

The Model, View, Controller pattern is actually quite simple (I'll be using game-like terminology since it's so easy to apply):
Model: State of the object (i.e. Foo['size'] = 100, Foo['health'] = 253, etc.)
View: Representation of the object (i.e. health bars, and the 3d mesh/skeleton/texture/material/shaders/etc)
Controller: Changes the object based on events (i.e. onUserInput, onCollide, onHit, setAnimation, etc)

Or, for web people:
Model: Stuff in database
View: Output HTML
Controller: PHP file

Of course, it can get more fine-grained than that since often you need to have separate models, views, and controllers for each kind of thing in the database for a single PHP file (i.e. user info and thread info).

Finally, don't put everything in a class just because you can. Contrary to popular belief, it doesn't make things cleaner. You still end up with globals and 'loose' static helper functions and whatnot, they're just all in a class (or, worse, more than one) now. Example: Just about anything written in Java.
^
MVC is always better, I hate those scripts where you have HTML inside PHP files and then SQL queries scattered everywhere...

vBulletin is a good example, they have MySQL queries everywhere, if you want to change MySQL to PgSQL, you'll literally need to hire a professional team to do so...
Pages: 1 2 3 4