• 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.
  • Posts

    • I wonder what $99 Chinese cellphone this phone will be based on. Whoever is dumb enough to buy the phone and/or get this service deserves everything they get.
    • Trump is a low-class guy who thinks painting everything in gold makes him look fancy.
    • Don't forget second season pass, that's not part of the superdeluxe edition.
    • Forza Motorsport gets IndyCar, Career mode improvements, and more by Taras Buria Turn 10 Studios announced the next Forza Motorsport update, which includes new content and various game improvements. Update 21 follows the previous Anniversary Update and adds IndyCar, a free expansion for Career mode, Featured Tours, new reward cars, and more. IndyCar is back in the game, allowing you to get behind the wheel of the latest-generation open-wheel racing cars. New vehicles include the 2025 Chevrolet IndyCar and the 2025 Honda IndyCar. There is also the all-new IndyCar Series in Featured Multiplayer. Career mode now has the Champions Cup, a new home for racecar-focused tours in single-player racing. There, you can drive a variety of modern and classic racecars from one-make series to endurance legends. Completing the Champions Cup will give you various high-reward cars like the 1979 BMW #6 BMW Motorsport M1 Procar, the 1998 Nissan #23 Pennzoil NISMO Skyline GT-R, the 2014 Dodge #93 SRT Motorsports Viper GTS-R, and more. Developers also adjusted the in-game pricing for most racecars to make the Champions Cup more accessible. Another change in Update 21 is the return of previous Featured Tours. The game will feature an additional Tour every month, allowing you to earn some of the previous reward cars and participate in the now-ended events. Here is a brief overview of other changes and improvements: Featured Multiplayer has been reorganized, incorporating player feedback, with the goal of providing players with even more event variety. Click here for a rundown of these changes. We’ve implemented BoP Spec changes to the Forza GT2, Forza GT3, Forza Touring Car, and Forza Proto-H divisions with help from participants in the Forza Insiders Program. See the release notes for an overview of the adjustments that have been made. The Challenge Hub menu has been reorganized to more effectively communicate where rewards can be earned as well as the type of rewards available to unlock. We’ve fixed an issue for players using the Fanatec DD2 racing wheel that would assign the peripheral an incorrect mapping profile. All Builders Cup Tours are now immediately unlocked, which means you no longer need to complete the Modern Tour to access the 3 other Builders Cup Tours. In addition, select Builders Cup Series have been updated to feature additional eligible cars and post-launch tracks. A detailed overview of these changes has been included in the release notes. There are also numerous fixes to AI behavior and competitiveness in single-player racing. You can read more about Forza Motorsport Update 21 in a post on the official Forza website.
    • If it ever gets to a reasonable level of parity, I think more real competition in the Windows space would be a good thing. What I like about this is that it's not just another reskinned Linux distro (with all of those inherent problems). It's meant to literally boot and work like Windows NT+. That could be an interesting development one day -- note that this has been in development since 1996, btw.
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      642
    2. 2
      ATLien_0
      274
    3. 3
      +FloatingFatMan
      173
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      139
  • Tell a friend

    Love Neowin? Tell a friend!