• 0

[php] oop tutorials with mysql


Classic or oop php?  

12 members have voted

  1. 1. Classic or oop php?

    • Classic
      6
    • oop
      6


Question

Hey,

I'm trying to learn php oop. I know some php, but adding in oop is a little confusing. I got the basics, but I need a simple tutorial that deals with mysql. connecting, selecting, inputting. I can't seem to find any good tutorials...

Thanks for any help :)

And I included a poll, just a little curious what the results will be.

Do you program using classic php (What I've heard it referred to a few times, not sure if thats the 'official' name or not) or oop?

Link to comment
https://www.neowin.net/forum/topic/865494-php-oop-tutorials-with-mysql/
Share on other sites

8 answers to this question

Recommended Posts

  • 0

What about some good books for oop php and mysqli? Ive found it surprisingly hard to found a good tutorial...I have a gift card for Chapters, so can pick up a book, just not sure which one everyone prefers or can recommend.

Thanks for any help :)

  • 0

"Classic PHP" as you call it is "officially" procedural coding.

I'm not sure what you are after? General MySQL tutorials or something specific to using MySQL in an object-oriented environment? If the former then there are loads of tutorials such as the one Andrew posted above. MySQL is used the same in procedural or object oriented coding methods.

If the latter then there is no specific or standard way that I know. You could simply have a separate object for handling database queries (typically referred to as a model) with methods for performing specific actions. This is how I tend to do it so that the database is separate from the logic, but not too abstract. A quick example:

class MyPostDatabase
  extends Database
{
  public function selectPosts($start, $limit)
  {
	return $this->query("SELECT title, body FROM posts LIMIT $start, $limit");
  }
}

$db = new MyPostDatabase;
$res = $db->selectPosts(0, 10);

The database class it extends contains a method called query, which performs the query using the many PHP MySQL functions and returns the result as an associative array (eg: $res['title']).

Or you can treat the whole database/table/query as an object. An example of this can be found in the Zend framework. I personally don't like this method as it complicates things unnecessarily in my opinion as it basically rewrites the whole MySQL language in PHP. It seems pointless to abstract things so much to me.

If you have more specific questions then there are plenty of people here that can help. :)

EDIT: This is the site I used when first starting out: http://www.php-mysql-tutorial.com/wikis/my...l-database.aspx

Edited by Fourjays
  • 0

You're going about this the wrong way. You're confused because you don't understand the concepts related to object-oriented programming.

Forget PHP and MySQL for a second. Read up on some OOP fundamentals then figure out how to apply those techniques using the language of your choice. Your code will be pretty awful at first, but you'll realize what was painful, and what worked. It's a process. Don't expect flawless results overnight.

  • 0

What I was looking for was in Fourjays' example. I'm trying to learn oop, and so wanted to do a small project, to help me understand the basics, but I find anything without a database, and dynamic boring.

I do know mysql, and php, but making it all an object, I found rather hard as I couldn't find any good tutorials. Trying to keep the php away from the html is tricky when having done it the other way for so long.

I found a script here, a dog example, that was great, but it didn't show how to show all your results. I believe that Fourdays' example has shown me how to do that, and has helped get me a little farther in my project.

sbauer - I believe you are very right about that. I think sometimes I try to get too far ahead of myself.

Thank you for the help :)

  • 0

As others have suggested, OOP and MySQL aren't something you learn together. Procedural (or what you call "classic") PHP consumes well written OOP PHP (the way I see it). OOP is just another programming tool to have. I like OOP because well written OOP code is easier to maintain, document, and extend the functionality of. OOP code can be difficult for beginners, because you have to think about your implementations in a different way.

If you don't have any web programming experience, start with simple procedural PHP and get the hang of programming for server side applications.

A good book I used for OOP in PHP is:

PHP 5: Objects, Patterns, and Practice

Kind of an old book, now, but I still refer to it sometimes. Another good resource to have for any OOP developer is Design Patterns. Elements of Reusable Object-Oriented Code.

Edit:

  Quote
I do know mysql, and php, but making it all an object

As an example of the power of OOP, you would not code a site such that it was a MySQL site. You would code a site such that it consumes some data from some database. You would implement abstract classes that define the interface you need to consume data from some data source. Then you would develop child classes that contain the specifics of the MySQL interactions to feed to your main program. This way, if you decide to change your mind and don't want to use MySQL anymore, you can develop another child class that defines the relationship between your interface and another data source. Then there is no need to change your main program that defines your application. Just swap classes.

If you jump in it like all your classes are going to be using MySQL, then you have missed a fundamental OOP concept of the abstract interface.

Edited by Shadrack
  • 0
  On 19/01/2010 at 14:03, Fourjays said:

--x--snip--x--

Or you can treat the whole database/table/query as an object. An example of this can be found in the Zend framework. I personally don't like this method as it complicates things unnecessarily in my opinion as it basically rewrites the whole MySQL language in PHP. It seems pointless to abstract things so much to me.

--x--snip--x--

Depending on the application, it can be completely pointless (or unnecessarily time consuming), or mission critical. Developers have to understand the application they are working on, know what tools they have and use their brains :).

It doesn't rewrite the whole MySQL language. It abstracts it so that (if you use it) adapting your code to other data sources is trivial. If you have ever had to convert code from one data source to another one, you will see that it is not pointless (for every project). But if you choose not to use abstraction on the data end, there is nothing to say you can't use abstraction on some other feature in your code.

I guess the points I'm trying to make are:

* Knowing OOP is a good thing

* OOP isn't necessary for every project

* Leveraging OOP's usability depends on the size and scope of a project. The larger a project is the better it will be if you implement OOP code.

* If I'm putting together something simple, that I don't plan to extend, I don't use OOP.

Edited by Shadrack
  • 0

Shadrack - Thank you very much :) Your messages are very helpful, and I think I do understand what you are talking about.

I do know some Procedural php, but thought it was time that I expanded on what I know.

I will take a look at those books as well :)

Thank you very much for your help :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.