Reputation: 21333
I hope you will help me! =)
Since I work with PHP, I always code procedural. Why? The answer is simple. I don't know how to work with objects etc.. But I want to! Really... =) I search the net and found this tutorial - Object Oriented Programming with PHP. So I read it, learned the syntax. A little bit experiment with given code. And I count it as learned! xD
Let me explain the problem. For example, I want to create users system. Before it was easy to do. I just create one file as HTML form. User input username and password. It's sends to another file where I validate it. If all is OK then PHP writes a new entry to the database. Otherwise error is created. The same with sign in, just for entry - session variable created with user ID. And how to check if user is sign in? Just check for session variable. No problems. And the problem comes... With objects? How can I do it? What's object (user, some validation?)? Where're objects? And so on with stupid, even newbie questions... =(
Could everyone create little example for me? Really for newbie in this. I really want to learn. Thanks. =)
P.S. Sorry for my English, it's not my native language. =P
Upvotes: 1
Views: 566
Reputation: 94
As a first step, you could just take a procedural script and make sure all large blocks of code are in a function. Then, put all functions that are similar or belong to each other in a different file. For example, group all user-related functions into user.php, database-specific functions to database.php, etc. If you have written procedural style for some time, you propably already have this.
Now the first step is to convert these files into classes:
Make the new user.php contain an empty class 'user'. Now, copy all user functions into this class (so they become methods).
Example:
before
function authenticate()
{
// auth
}
function createUser()
{
// create new user
}
after
class person
{
public function __construct()
{
// create new user
}
public function authenticate()
{
// auth
}
}
If you do this for all groups of functions, you end up with a very basic Object Oriented program.
Example:
instead of calling $user = createUser('john doe');
you just do $user = new user('john doe');
I recommend you to practice it this way, just with a simple script to begin with. Learning OO starts with doing it.
when you have finished your first OO-like program, look for things to improve. Is one class getting way too big? Split it in multiple sub-classes. Are multiple classes doing nearly the same? Maybe let them all inherit one base-class.
In short: just start doing it. It is not all that different. Imagine a class as a buch of functions.
Whenever you walk into a situation where you feel something is wrong with your code, look for a guide with best practices or examples for OO (not specifically for PHP), there are plenty.
P.S. as some of the answers point out, i don't think you should not expect someone to write half your app. Just try to make a start and post a question if it isn't working.
Upvotes: 1
Reputation: 1231
Well starting using OOP doesnt really change the flow your describing although most of your logic which would normally be on the pages will now be inside the objects.
So your user class could exist of a constructor which will get the user from the database if an id is given, edit and save options. Just start making a user class and think of usefull methods you would like to call inside your class.
You might also want to start reading a bit about design patterns, this would probably help you on making your classes aswell. The first class you probably want to make is a class that will give you your database connection this can be done very easily with a singleton pattern. Start reading about that one its pretty basic to grasp and very usefull.
Upvotes: 0
Reputation: 60403
This is an extreemly broad question. Youre asking us to give you a skeleton implementation of the core of an entire application! :-)
Most of this also depends on how you want the architecture of your app. YOU have to create all these classes or use open source implementations of them, they are not built in to PHP. That is why most people use a framework that provides most of this base stuff.
However I wouldnt recommend jsumping directly to using a framework with your base level of knowledge. ID urge you to read up OOP patterns and concepts. This article while old gives a direct comparison of moving from procedural to OOP and walks you through creating a basic MVC framework of your own. Ther are lots of resources similar to this so i would google if i were you.
I dont know that i would use such a framework in production but the process of creating it and making soem personal apps with it is invaluable to learning. After you get some of the key concepts and patterns down then i would choose a framework thats got a lot of structure (in your case the more rigid the better, since youre just learning) and begin working with that.
While i dont personally use it because its not flexible enough I think i would recommend CakePHP in your case. Symfony might also be a good choice, but its extremely complex and if youre new to OOP you may lose your mind... Ditto for Zend :-)
I would also recommend PHP Objects, Patterns, and Practice, Second Edition as a general reference book. Its similar to PoEAA but specific to php implementations and examples.
Upvotes: 3