0

I am having a strange issue with my php code. This is the php version I am using PHP 5.3.2-1ubuntu4.30 with Suhosin-Patch.

The issue I am having is that I cannot assign an anything to a variable that is part of my class

namespace Stats\POTSPortStats;


use PDO;
use Stats\Port;
use Stats\POTSPortStats\IPHosts\IPHosts;

class PortStats extends Port
{

    public $vcPortOnHookStatus;
    public $vcPortImpedance;
    public $ipHosts;

    /**
     * @param $secretValue int
     * @param $pdo_conn PDO
     * @return PortStats[]
     */
    public static function getAll($secretValue, $pdo_conn){
        try {
            $query =
                "SELECT * FROM secretTable WHERE secretColumn = :secretValue";
            $pdo_stmt = $pdo_conn->prepare($query);
            $pdo_stmt->bindValue(":secretValue", $secretValue, PDO::PARAM_INT);
            $pdo_stmt->execute();

            /** @var PortStats $result */ //This works right
            $result = $pdo_stmt->fetchAll(PDO::FETCH_CLASS, __CLASS__);

            //other is assigned an Array of IPHosts classes
            $other = IPHosts::getAll($secretValue, $pdo_conn);
            echo json_encode($other);
            //Checked for result, it is there.
            $result->ipHosts = $other;

            //Check just assigned value
            var_dump($result->ipHosts);
            //Value was NULL
        } catch (\PDOException $e) {
            //nothing here
            var_dump($e);
        }
        //The rest of the result is returned correctly
        return $result;
    }

Is there something wrong with my code that I am overlooking? Is there some kind of bug inside of this version of PHP that I did not find when I looked? Any help would be much appreciated. Please ask if you need more clarification.

9
  • Any errors showing? More importantly is error reporting enabled? Commented Jul 11, 2016 at 13:43
  • I don't see any errors. Commented Jul 11, 2016 at 13:43
  • Ensure that PHP is set to display all errors: ini_set('display_errors', '1'); and error_reporting(E_ALL);. Commented Jul 11, 2016 at 13:45
  • 1
    I don't see any assignments to public properties of class Commented Jul 11, 2016 at 13:45
  • If you are trying to change a database value like that, it won't work. You need to use UPDATE. Also remember your variables are non-static yet your function is static. Commented Jul 11, 2016 at 13:45

2 Answers 2

4

I think the problem is in this line:

$result = $pdo_stmt->fetchAll(PDO::FETCH_CLASS, __CLASS__);

Are you sure that pdo_stmt->fetchAll(...) returns an object of type PortStats? I don't think it does. It returns an array. And even more so, I don't think you can implicitly cast to class PortStats.

Try:

$results=$pdo_stmt->fetchAll(PDO::FETCH_CLASS, __CLASS__);
foreach($results as $result){
    var_dump($result);
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is what I was thinking I haven't had the chance to test it yet though
I haven't tested it yet, but I am pretty confident that this is what the problem was, It is amazing how it is possible to miss these kind of things, and see it like a minute after you post it.
It should have been /** @var PortStats[] $result */ and then change some code in the IPHosts class
@DreamBlob OK everything works now, Spent 2 hours staring at that code. Thanks for the help!
0

I think there might be an issue on the following line.

$other = IPHosts::getAll($nCircuitStatsID, $pdo_conn);

I can't see $nCircuitStatsID being defined anywhere else in your code. Is it a global?

I would also expect fetchAll() to return an array of type __CLASS__ rather than an instance of __CLASS__.

2 Comments

That is the same as $secretValue, forgot to change it.
In that case, I expect the problem is the same as @DreamBlob outlined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.