Procedural code rewriting error for OOP

Asked

Viewed 75 times

0

The archive wishlist.php available in GIST should, through file methods db.php, perform actions of CRUD and print on the screen the obtained results, 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, it happened when I adapted it to OO. I believe that I’m letting something very simple pass in blank, and as I’m beginner with programming I’m not being able to notice.

Codes:

  • 1

    Put in the file where the screen is blank at the beginning: ini_set('display_error', true); error_reporting(E_ALL);

  • @rray insert, but no error was reported.

  • @rray the page is neither loading nor the form; as if there were no code on the page. When I remove the script require it and the $wisherID = WishDB:: ... the form works.

1 answer

3


Blank screen is an indication that an error has occurred and the production server usually hides the errors for security reasons. For quick repair or debug in production just add two lines one that displays errors and the other one that indicates which ones should be shown. More information about errors see on 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: Cannot override final method PDO::__wakeup()

This is a magical and final which cannot be overwritten, so comment or remove it.

Fatal error: Access level to Wishdb::__Construct() must be public (as in class PDO) in

As the class db inherits from PDO you cannot lower the access level a method/property of the parent class. As the error suggests let the constructor of dbas an audience.

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

The result of the listing cannot be obtained because its method returns a Boolean(return from execute() instead of an array. In case add the 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 the return $consult->execute(); according to the code below.

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

In the listing file, change:

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

for:

$result = WishDB::getInstance()->getWishesByWisherId($wisherID);
foreach($result as $row)
  • thanks for the help!! Thanks also for the tip!

  • Nothing when you need it, solved everything? I haven’t tried the Insert part. @Luan.

  • I managed to tidy up; with the tips you gave me I could see some other mistakes. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.