SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 38 of 38
  1. #26
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    But the challenge is to pass around the database connection in a procedural style without using globals. This function would do it - if you call it after the db is created, you'll just receive the handle. If the handle isn't created already, the connection will be made and the handle will be returned.

    I'm not sure I agree a new connection should be created each time anyway. It'd be more resource heavy, unless you're planning to just pass the database handle around all over the place?
    1) I'm not participating in your challenge.
    2) My way does not require a new DB connection, it requires one. You reuse the object that was created.

  2. #27
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    1) I'm not participating in your challenge.
    2) My way does not require a new DB connection, it requires one. You reuse the object that was created.
    1) It wasn't MY challenge - it was the question we were all actually answering.
    2) If I call your function two times, I create two database connections, which is more resource heavy than just returning the database handle on the second function call.
    3) Passing the database handle around may not be such a bad idea - I was just pointing out that your method would be more resource heavy if it were not used this way. Personally, I use Doctrine for almost everything these days, so haven't had to think about old school database handles in some time...

  3. #28
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    2) If I call your function two times, I create two database connections, which is more resource heavy than just returning the database handle on the second function call.
    Your not using it correctly then. Call the resource, use the same resource over your script (if you want to) or in some cases as I quite often do, I decide to open a second connection for performance reasons. You can do 1 connection to run all your operations or 10. Its up to whoever is calling it.

  4. #29
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to somewhat hijack the thread, but of those of you who use procedural, do you separate your code and organize it well? I separate PHP from HTML with procedural, functions are included, and overall it's pretty organized.

    PHP Code:
    $link mysqli_connect('localhost''user''pass');
    mysqli_select_db($link'database');
    function 
    WTF($link) { 
        
    $Q mysqli_query($link"SELECT * FROM Users LIMIT 1");
        while(
    $R mysqli_fetch_assoc($Q)) {
            
    print_r($R);    
        }

    }
    WTF($link); 
    Considering how much databases are used with anything, I certainly don't mind providing the link. Is it really a big deal? I agree that procedural is noob if that's all that is ever used, but also that it is great for a quick script with small complexity. I'm not a pro, so not like my opinion really matters!

  5. #30
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I have read many articles on OOP in hope to learn OOP and most of them say that functions inside a class is OOP.
    I am not able to encounter any good article on OOP which really teaches on how to do OOP programming properly.

    Can you experts suggest any ?

    For now my aim is to learn:

    1. how to connect to db using oop
    2. how to run sql queries. You know i have seen code like $db-> (dunno how to do that)
    3. how to fix/sanitize user data.
    4. How to fetch a single and multiple records.

    Whenever I try i fail to understand it and i go back to procedural method.


    Thanks.

  6. #31
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,264
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tapan View Post
    Hi,

    I have read many articles on OOP in hope to learn OOP and most of them say that functions inside a class is OOP.
    I am not able to encounter any good article on OOP which really teaches on how to do OOP programming properly.

    Can you experts suggest any ?

    For now my aim is to learn:

    1. how to connect to db using oop
    2. how to run sql queries. You know i have seen code like $db-> (dunno how to do that)
    3. how to fix/sanitize user data.
    4. How to fetch a single and multiple records.

    Whenever I try i fail to understand it and i go back to procedural method.


    Thanks.
    I frequently hear smart people recommend the book "PHP Objects, Patterns, and Practice" by Matt Zandstra. I've only skimmed it myself, but it looks to be a very good book. It teaches not only the basics such as syntax, but it also teaches how to think and design in an OO style. The latter is just as important as knowing the syntax, otherwise you may never get past thinking of a class as just a bag of functions.
    "First make it work. Then make it better."

  7. #32
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    I frequently hear smart people recommend the book "PHP Objects, Patterns, and Practice" by Matt Zandstra. I've only skimmed it myself, but it looks to be a very good book. It teaches not only the basics such as syntax, but it also teaches how to think and design in an OO style. The latter is just as important as knowing the syntax, otherwise you may never get past thinking of a class as just a bag of functions.
    Hi,

    Thanks, i will check it out.

    Thanks.

  8. #33
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tapan View Post
    Hi,

    Thanks, i will check it out.

    Thanks.
    It's a great book.

    It's quite dense though, and you probably won't "get" everything right away. It takes time.

    I'd say I really "got" objects for the first time when I first understood why one might use polymorphism in practise. Once I understood that (and it took me a while - for a long time, my objects were just basically procedural code wrapped in classes), the world of objects started making a lot more sense to me.

    Once you understand why the phrase "code to an interface, not an implementation" makes so much sense, you'll be on your way to getting it. I'm in work now so can't type up a proper example, but perhaps later I'll have a go at showing you a few examples... See what other people in here can come up with too :-)

  9. #34
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Quick n dirty list of benefits of object oriented design. The main bullet point of OOP is less code (if done right). From there we can get these much more important points:


    1. Readability - You may not think so since your new to it, but I kill over being handed a large procedural project management wants modified.
    2. Ease of changes - Make changes in once place rather than many
    3. Stability - Less chance for slightly different code being produced in multiple places. This is a HUGE deal with bigger projects. HUGE. Did I mention HUGE? A bigger PHP project without strict OO concepts is the first step toward failure.


    Beginner Example:

    Imagine writing an application that has to make a database connection multiple times (across multiple scripts). Each time you write it out you have at least 2-3 lines dedicated just to the connection. If you ever change credentials or location of the database, you'd have to come back to each page and make the change. A proper OOP setup would mean you only initiate a new DB object that you created which handles your connection for you. Now you or another developer only needs to make a change in one location rather than 10.
    You missed one of the biggest benefits (IMO) - the ability to unit test applications easily, which is hugely important.

  10. #35
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    You missed one of the biggest benefits (IMO) - the ability to unit test applications easily, which is hugely important.
    I'm wondering whether it might be worth setting up a vagrant virtual machine with some unit tests and some basic objects to show people on here how objects can be so useful...

    Problem is I'm always so busy with work, but if there's interest in it, I may have a go at some point (perhaps some other people on here could help out?)... it could be a really cool exercise... I'm thinking we create a virtual machine with phpunit and unit tests setup, as well as some examples of how objects can be used in practice and host it on github somewhere, then provide a tutorial of sorts on here...

    If people are interested in the idea, let me know... perhaps we can work something out together?

  11. #36
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by c.nickerson View Post
    seems like procedural is more commonly used still
    I wouldn't say that is true really. Perhaps amongst amateur projects / hobby projects, and big names that have to deal with legacy, but don't think it's true for big projects, a lot of enterprise, or most of the open source community at all.

  12. #37
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    I frequently hear smart people recommend the book "PHP Objects, Patterns, and Practice" by Matt Zandstra. I've only skimmed it myself, but it looks to be a very good book. It teaches not only the basics such as syntax, but it also teaches how to think and design in an OO style. The latter is just as important as knowing the syntax, otherwise you may never get past thinking of a class as just a bag of functions.
    I have the book "PHP 5 Advanced" by Larry Ullman. His books are great he is very good at CLEARLY describing and showing examples of how to do things. This book covers OOP PHP in a few chapters and explains that OOP isn't just a syntax change but a change of coding theory.

    P.S. if there is ever a newb trying to learn PHP, I recommend "PHP 6 and MySQL 5" by Larry Ullman. Book covers everything from echo to creating an ecommerce system.

  13. #38
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Stormrider View Post
    I wouldn't say that is true really. Perhaps amongst amateur projects / hobby projects, and big names that have to deal with legacy, but don't think it's true for big projects, a lot of enterprise, or most of the open source community at all.
    Indeed. The fact that Procedural style is more commonly used in PHP projects is a mere indication that there are a lot more beginner/amateur coders using this language. Apparently PHP is easy for newbies to start with, so it makes perfect sense to me. After all, true professional/advanced coders all use OOP over Procedural any time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •