glassfish Posted October 29, 2014 Share Posted October 29, 2014 (edited) Script: <?php class Article{ public function fetch_all(){ global $pdo; $query = $pdo->prepare("SELECT * FROM `articles` ORDER BY `article_timestamp` DESC"); $query->execute(); return $query->fetchAll(); } public function fetch_data($article_id){ global $pdo; $query = $pdo->prepare("SELECT * FROM `articles` WHERE `article_id` = ?"); $query->bindValue(1, $article_id); $query->execute(); return $query->fetch(); } /* * These have gotten added later on. */ public function delete($id){ global $pdo; $query = $pdo->prepare("DELETE FROM `articles` WHERE `article_id` = ?"); $query->bindValue(1, $id); $query->execute(); // The "return" here. } public function edit_page($title, $content, $aticle_id){ global $pdo; $query = $pdo->prepare("UPDATE `articles` SET `article_title` = ?, `article_content` = ? WHERE `article_id` = ?"); $query->bindValue(1, $title); $query->bindValue(2, $content); $query->bindValue(3, $article_id); $query->execute(); // The "return" here. } } ?> I have this from a tutorial. In the function "fetch_all()" it uses "fetchAll()" at the spot where the "return" is, and in the function "fetch_data()" it uses "fetch()" at the spot where the "return" is. My Question: What may have to use for the function "delete()" and the function "edit_page()" at the spot where "return" would be? Edited October 29, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/ Share on other sites More sharing options...
Ch0cu3r Posted October 29, 2014 Share Posted October 29, 2014 I have this from a tutorial. Its not a good tutorial if it is defining $pdo as global! In fact all it has doing is wrapping procedural code inside of a class. What may have to use for the function "delete()" and the function "edit_page()" at the spot where "return" would be? A boolean value maybe? Return true if the article has been deleted or the article has been updated. Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/#findComment-1495148 Share on other sites More sharing options...
ginerjm Posted October 29, 2014 Share Posted October 29, 2014 (edited) Personally I think your functions all need some work. Nowhere do you make any provisions for error handling. Sure your pdo may issue an exception that goes to the error log but what does your appl do to handle that? Furthermore - when a query doesn't return a result - how are you prepared to handle that? Add some logic in each function to test for success or failure and to test if you actually have any rows to return. Be sure to modify your calling code to handle whatever results you may then return. Perhaps return false if the query fails or returns no data, or return the fetched data if it does find something. As for the delete - as chocu3r says - return true or false - BUT after you add some logic to check what the delete query actually did. Time to do some reading. Edited October 29, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/#findComment-1495150 Share on other sites More sharing options...
glassfish Posted October 29, 2014 Author Share Posted October 29, 2014 (edited) Its not a good tutorial if it is defining $pdo as global! In fact all it has doing is wrapping procedural code inside of a class. Do you have a suggestion? I really am trying to learn OOP. I thought I could be learning good with "CRUD". Do you have "something" where I could be seeing the "difference" with what you have had mentioned? A boolean value maybe? Return true if the article has been deleted or the article has been updated. Are you also saying that the "execute" part will do this already? Though, I understand that the return of "true" would happen on success. ginerjm, Thanks for the answer. I understand what you are saying, though I thought I could be learning OOP with a "CRUD" tutorial. I also feel like after those few tutorials I could be going ahead and read the books. Edited October 29, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/#findComment-1495152 Share on other sites More sharing options...
ginerjm Posted October 29, 2014 Share Posted October 29, 2014 Simply pass the db connection var in as an argument of your methods instead of declaring it globally. That way your class/methods are portable and not locked into only using scripts where the db connection is identified as $pdo. Read up on function arguments/parameters. What is it you 'understand (I am) saying'? I told you you need to include better process handling in your code. Think about it. If you are being given instructions on how to do something yourself and you run into a roadblock, you need to know what to do in that case, correct? So when writing code you need to think like that and ask yourself -what do I want to do if x occurs. Or if y occurs. Or if z occurs. A good programmer covers all the bases and tries to prepare his system to handle every reasonable eventuality. You didn't do that in this example. You just programmed along and didn't consider that anything could not do what you wanted it to do. It's like going to the store for white bread and they don't have any and not being prepared to buy anything in its place. Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/#findComment-1495160 Share on other sites More sharing options...
glassfish Posted October 29, 2014 Author Share Posted October 29, 2014 You wrote: "Time to do some reading." Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/#findComment-1495162 Share on other sites More sharing options...
glassfish Posted October 29, 2014 Author Share Posted October 29, 2014 (edited) Addendum: To what you wrote after that: I did not even think this code would be poor. It also disappoints. On "Stackoverflow" one gets told that "90% of the tutorials out there are poor". I get to think this could be true, yet it also disappoints. One of the comments on the tutorial: This tutorial is rather to confuse newbies and to teach wrong techniques. I will pay more attention to this. Edited October 29, 2014 by glassfish Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/#findComment-1495165 Share on other sites More sharing options...
Strider64 Posted October 29, 2014 Share Posted October 29, 2014 (edited) There is a good book called "PHP Objects, Patterns, and Practice" by Matt Zandstra (Fourth Edition) that is pretty good on OOP and more. I would consider doing iCRUD interface for it forces you to structure your OOP. <?php /* The iCrud interface. * The interface identifies four methods: * - create() * - read() * - update() * - delete() */ interface iCrud { public function create($data); public function read(); public function update($data); public function delete($id=NULL); } Besides then you can swap your classes if you move on to a different project. I know books can be tedious, but the only true way I found how to learn the material is either via books or by taking a college course on the subject. Just my .02 cents. Edited October 29, 2014 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/#findComment-1495166 Share on other sites More sharing options...
KevinM1 Posted October 29, 2014 Share Posted October 29, 2014 Yeah, the Matt Zandstra book is required reading if you really want to do OOP in PHP. After that, you should get the Gang of Four's book, which really drives the point(s) home. Both can easily be found on Amazon. They're basically OOP 101. Quote Link to comment https://forums.phpfreaks.com/topic/292137-php-oop-class-return/#findComment-1495170 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.