OOP PHP and MySQL
Hi I'm new to OOP with PHP. I'm wondering how OO comes into play when selecting da开发者_开发百科ta from a table. i.e. i have a users table and a user profile page. So I have a User class. Do I select the user data from the table then create a new object of class User and define the properties?
Thanks for anyone that can clear this up for me!
Regards,
Jonesy
Use ORM, like Doctrine or Propel.
See also Stackoverflow question "Good PHP ORM library"
That's it, you can create a UserManager class, that selects user data and create User object(s) (depending on you WHERE clause) with it. This class could have the following methods : selectUserById(), selectUsersByAge(), etc...
You can create a class to build MYSQL queries that can give you data from your DB, for example, you might have a method that give you a user by passing a parameter to that method, or you can have another method that give you the number of users in your table for example.
you can build your class in the way you need it.
After define a new instance of your class in your page you can use it to return data from your DB using the methods you defined them in your class and use it in your page as you want.
I hope this can help you.
精彩评论