• 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

    • Get Windows 11 Home or Pro for only $9.97 with this coupon by Steven Parker Today's highlighted deal comes via our Apps + Software section of the Neowin Deals store, where you can save up to 92% off on a Microsoft Windows 11 Home, or Pro license. Upgrade your computing experience with Windows 11 Pro. This cutting-edge operating system boasts a sleek new design and advanced tools to help you work faster and smarter. From creative projects to gaming and beyond, Windows 11 delivers the power and flexibility you need to achieve your goals. With a focus on productivity, the new features are easy to learn and use, enhancing your workflow and efficiency. Whether you're a student, professional, gamer, or creative, Windows 11 Home has everything you need to take your productivity to the next level. New interface. easier on the eyes & easier to use Biometrics login*.Encrypted authentication & advanced antivirus defenses DirectX 12 Ultimate. Play the latest games with graphics that rival reality. DirectX 12 Ultimate comes ready to maximize your hardware* Screen space. Snap layouts, desktops & seamless redocking Widgets. Stay up-to-date with the content you love & the new you care about Microsoft Teams. Stay in touch with friends and family with Microsoft Teams, which can be seamlessly integrated into your taskbar** Wake & lock. Automatically wake up when you approach and lock when you leave Smart App Control. Provides a layer of security by only permitting apps with good reputations to be installed Windows Studio Effects. Designed with Background Blur, Eye Contact, Voice Focus, & Automatic Framing Touchscreen. For a true mouse-less or keyboard-less experience TPM 2.0. Helps prevent unwanted tampering Windows 11 Pro also includes a number of productivity-focused features, such as the ability to snap multiple windows together and create custom layouts, improved voice typing, and a new, more powerful search experience. Personal and professional users will enjoy a modern and secure computing experience, with improved performance and productivity features to help users get more done. Only on Windows 11 Pro If you require enterprise-oriented features for your daily professional tasks, then Windows 11 Pro is a better option. Set up with a local account (only when set up for work or school) Join Active Directory/Azure AD Hyper-V Windows Sandbox Microsoft Remote Desktop BitLocker device encryption Windows Information Protection Mobile device management (MDM) Group Policy Enterprise State Roaming with Azure Assigned Access Dynamic Provisioning Windows Update for Business Kiosk mode Maximum RAM: 2TB Maximum no. of CPUs: 2 Maximum no. of CPU cores: 128 Good to know This license is for Windows 11 only. It is NOT intended to be used for upgrading Microsoft Office (MSO) included in Parallels Pro. However, it will still work with Parallels Pro and allow you to run Windows applications including MSO, but it DOES NOT include an upgrade MSO itself. It is still compatible with Microsoft Office ONLY if you have a separate license for it. Length of access: lifetime Redemption deadline: redeem your code within 30 days of purchase Access options: desktop Max number of device(s): 1 Version: Windows 11 Pro Updates included Click here to verify Microsoft partnership For example, a Microsoft Windows 11 Pro license normally costs $199, but you can pick it up for just $14.97 for a limited time, that represents a saving of $184. For a full description, specs, and license info, click the link below. Get Windows 11 Home or Pro for just $9.97 use MSO5 coupon when checking out Coupon expires on June 29. Although priced in U.S. dollars, this deal is available for digital purchase worldwide. We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • I hoped it was a bug and will be fixed. Why haven't they made so it asks for a PIN in the dark then? It tries to use the camera for several minutes or I have to manually select PIN and the next day in the morning I'll have to manually select the camera back, because it will just be sitting and waiting for a PIN. It just remembers the last used method regardless of the lighting.
    • I would recommend PowerToys just to remap a Copilot button on modern laptops for something more useful than Microsoft allows in Windows Settings. I also use some other tools like "Always on Top" and "Crop and Lock".
    • Makes sense, we know MS doesn't take dark mode seriously, lol.
    • Why would you think Johny Ives is behind this ? he has not worked at Apple or on a smart Phone or Phone in general for long time On 27 June 2019, Apple announced that Ive would depart Apple after 27 years
  • Recent Achievements

    • 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
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      640
    2. 2
      ATLien_0
      275
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      155
    5. 5
      Steven P.
      138
  • Tell a friend

    Love Neowin? Tell a friend!