Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
jeffjeffs

Any Tutorials On Converting Procedural Code To Oop?

Recommended Posts

i used the larry ullman book php and mysql many years back and made the websites using procedural code.

 

now every tom dick and harry says to use OOP but can't be bothered to give proper tutorials.

 

for example, in the old book, i followed the tutorials in creating a website which has username/passwords and accounts.

 

now if i want to make this in OOP where the hell do i start from???

 

its all well and good giving examples on OOP but how am I supposed to figure out how to create the same existing code into OOP???
 

i'm at a total loss and extremely pissed off that i learned all this procedural stuff, and now everyone is saying to use OOP

 

well if there are no simple tutorials to follow (for example building the same websites as in the old books) in OOP then i what am i supposed to do??

 

does this new PHP advanced book actually give a tutorial step by step in building a website with those features or am i expected to just guess and find out miraculously??

 

most of the tutorials on the web are completely useless, especially ones on blogs and NetTuts and other garbage websites.

 

once you read those tutorials, there are always tons of comments below them outlining why they are wrong code, adding further to the confusion, and putting me off completely.

 

I hope someone can make a book on PHP OOP with clear easy to follow examples and a tutorial where you actually build something in it.

 

and how about something like multiple image uploads per ID (ie for a car or real estate website).

I struggled to find out how to do this , and that was in Procedural. half the code i got was cobbled together and it seems people are so tight, they don't want to share tutorials on how to this do this properly.

 

 

Its all well and good telling people to go and read a book, but if the book doesn't give you proper actual examples which we can use, then whats the point???

 

 

Share this post


Link to post
Share on other sites

It's unfortunate that this posting didn't attract any replies (probably because it's too much of a rant). It does however reveal a real problem. I too have 'grown up' on Larry's books (and others by Sitepoint) that cover PHP and MySQL thoroughly in procedural code. I have created several web sites based on what I've learned.

 

I'm now ready to move on to OOP, but despite reading about OOP and understanding the advantages I cannot get a handle on where to start re-writing my code. It's not even clear to me what I should choose as my classes. There's some guidance to be found on the web, but the tutorials I've found concentrate mainly on connecting to the DB using OOP and PDO, and only in passing on what to do after that.

 

I came (back) to this forum to see if I could find out if the current edition of this book covers OOP and MySQL together. I've read the Contents (on Amazon) but that hasn't convinced me that I'll find what I'm looking for. Hence my search through the topics.

 

I had thought that Larry had retired from book writing to take up employment, but I see there's a revised book coming out later this year, so perhaps there's hope yet?

 

Tim Dawson

 

 

Share this post


Link to post
Share on other sites

Hey Tim! To rewrite a procedural application in OOP--if that's something you feel is necessary or just want to do--start by thinking about what "things" exist. In a database-driven application, the things often correlate to the database tables. Another good way to find your application's things is to think about what the users and administrators might do. If a user registers, logs in, updates preferences, and logs out, the user is a thing. If a user views a page and an administrator creates and updates pages, a page is a thing. 

 

The things then become classes. 

 

"Services" can also be things which in turn leads to classes. For example, the database (leading to PDO) or the outputted web page (leading to templating classes). 

 

This leads to the fact that because PHP supports both procedural and OOP, you don't have to do a full OOP implementation, especially when you're just getting started. For example, you can refactor "page" or "user" as a class as a step. Or the database interactions via PDO. 

 

Let me know if you have any other questions and good luck!

Share this post


Link to post
Share on other sites

Hello Larry,

Thanks for your reply. That makes sense to me. I guess I'll just have to feel my way from there. I don't think it's necessary to re-write my existing procedural code, but it may be easier to understand what I'm doing that way than starting with a new web site.

 

It would seem my DB tables have a lot of common features (e.g. methods for SELECT, INSERT, UPDATE, etc.) so I'm thinking an abstract 'table' class might be the way to go, then each actual table class would extend the 'table' class? That's what I'm experimenting with right now. I can see that my abstract table class might be reused in another project, but the extended classes are going to be more project specific because they'll contain properties related to the fields in the DB, which may differ somwhat between projects.

 

When it comes to displaying something from the DB (having selected), that would be a page class, perhaps? But it would be a very specific page (possibly with an HTML table, though not in all cases). One of my sites lists holiday accommodation, so it would seem  'listing' would become a templating class? Do I then have to have an object for each specific listing on the page? It's all dynamic, so the object names are going to have to follow from the listing ID. Example at: http://holidaymull.co.uk/scs-sw

 

I know that's a lot of questions, and I'll understand if you haven't time to respond. I expect I can work out most of the answers in time! I really hope this sort of 'what do I do next' advice can be included in the new version of this book.

 

Best Wishes,

Share this post


Link to post
Share on other sites

Hey Tim! No problem on the questions! 

 

I wouldn't worry about representing the concept of a table and then the table interactions as classes. That's what PDO can do already. But you are right on the other side of things: a listing sounds like an object! So you'd store listings in the database and fetch them using PDO and you can fetch them into specific class types, in this case Listing (or whatever you name it).

 

It sounds like that's a pretty good place to start!

Share this post


Link to post
Share on other sites

Thanks Larry.

 

I've recently been working from a PHP/OOP/MySQL tutorial that used mysqli_connect syntax rather than PDO, so this may be why the author wrote an abstract table class with SELECT, INSERT UPDATE functions, etc. I'll revisit PDO. At least I have learnt something by making that work for me.

 

I'll also have a go at a 'listings' class.

 

Best Wishes,

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...