Jump to content

PHP OOP Class Return


glassfish

Recommended Posts

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 by glassfish
Link to comment
Share on other sites

 

 

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.

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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 by glassfish
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by glassfish
Link to comment
Share on other sites

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.  ;D

Edited by Strider64
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.