Adam Forbis Adam Forbis - 1 year ago 93
PHP Question

PHP class variable not assigned

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);

/** @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
//Value was NULL
} catch (\PDOException $e) {
//nothing here
//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.

Answer Source

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.


$results=$pdo_stmt->fetchAll(PDO::FETCH_CLASS, __CLASS__);
foreach($results as $result){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download