Error in procedural code rewrite for OOP

0

The wishlist.php file available in GIST should, via methods from the db.php , perform CRUD actions and print the results on the screen, but nothing is printed. Likewise, the file createNewWisher.php should also perform operations of the type, but the screen turns white.

The system works perfectly in procedural mode, this happened when I adapted it to OO . I think I'm letting something very simple go blank, and as I'm a beginner with programming I can not figure it out.

Codes:

asked by anonymous 13.06.2015 / 02:07

1 answer

3

Blank screen is an indication that some error has occurred and the normally production server hides the errors for security reasons. For quick repair or debugging in production, simply add two lines one that displays the errors and the other one that indicates which of them should be shown. More information about errors see php wiki .

ini_set('display_error', true);
error_reporting(E_ALL);

I tested your listing code and it showed the following errors in class db

  

Fatal error: Can not override final method PDO :: __ wakeup ()

This is a magic method and final that can not be overwritten, then comment or remove it.

  

Fatal error: Access level to WishDB :: __ construct () must be public (as   in class PDO) in

Because the db class inherits from PDO you can not decrease the access level of a method / property of the parent class. As the error suggests let the db constructor be public.

class WishDB extends PDO{
   //propriedades omitidas
   private function __construct(){

The result of the listing can not be obtained because its method returns a boolean (return from execute() instead of an array. In case add error handling and an array

public function getWishesByWisherId($wisherID)
{
    $consult = $this->prepare("SELECT 'id', 'description', 'due_date' FROM 'wishes' WHERE 'wisherid'=:wisherid");
    $consult->bindValue(':wisherid', $wisherID);
    return $consult->execute();
}

Remove return $consult->execute(); as per the code below.

    if($consult->execute() === false){
        print_r($consult->errorInfo());
    }

In the list file, change:

$result = WishDB::getInstance()->getWishesByWisherId($wisherID);
while ($row = $result->fetch(PDO::FETCH_ASSOC))

by:

$result = WishDB::getInstance()->getWishesByWisherId($wisherID);
foreach($result as $row)
    
13.06.2015 / 03:38