Trouble calling function in another class/file in PHP

76 Views Asked by At

I have a class called "Stock" in foo.php:

class Stock
{
    public static function productExists($productsId)
    {
        $connection = TestDb::getConnection();
        $query = $connection->prepare(self::SQL_GET_PRODUCT_BY_ID);
        $query->execute([':products_id' => $productsId]);
        $product = $query->fetchAll();
        if ($product === false) {
            throw new SQLException();
        }
        return (count($product) > 0);
    }
}

I am trying to call it from another php file in the same directory. The code is this:

<?php

require ('foo.php');
$myClass = new Stock();
$res = $myClass->productExists(1000);
echo $res

I get an error:

PHP Fatal error:  Call to a member function execute() on boolean in /Users/totalnewb/Documents/workspace/company/company_BE_QA_Script.php on line 286

Line 286 is line 7 in the above example. I have value in the database for that table/column that should return a boolean of 1 as I read it. The static SQL query referred to is fine and the DB connectivity is fine. I cannot touch the code in the Stock class. I know this is basic stuff so I am a bit baffled.

My other question is how come I can't put an echo statement into productExists to see the actual prepared statement? When I add echo $query; after the execution it doesn't echo anything.

0

There are 0 best solutions below