Fabiotk Fabiotk - 1 year ago 133
PHP Question

Can't access value of protected static variable php 7.0.13

I'm trying to implement a DataTable class in php which will be basically a table to contain data just like an sql table and will be made of array()

The class is defined as followed:

class DataTable{

protected static $tabela; // table
//columns and strips, each column point to a strip
public function __construct($colunas, $faixas) {

$this->tabela = array();
$this->constroiDt($colunas, $faixas); //builds the table


public function getRows($where){
// todo

public static function getTabela(){
return $this->tabela;

private function constroiDt($colunas, $faixas){
if(count($colunas)!= count($faixas)){
$this->tabela = null;

foreach($colunas as $cl){
$this->tabela = array_merge($this->tabela, array($cl => $faixas[$i]));
$i += $i + 1;
// the result will be an array like ('col1' => ('val1','val2'), 'col2' => ('val1','val2'))
// if I want to access a value, just type array['col1'][0] for example


Outside of the class function, when I create an example DT and try to access it, it seems that will work:

$columns = array("name", "age");
$strips = array(array("someone","another person"),array("20","30"));
$dt1 = new DataTable($columns, $strips);
var_dump($dt1); // will print:
// object(DataTable)#1 (1) { ["tabela"]=> array(2) { ["nome"]=> array(2) { [0]=> string(6) "fulano" [1]=> string(7) "ciclano" } ["idade"]=> array(2) { [0]=> string(2) "20" [1]=> string(2) "30" } } }

But then I add
echo "--- " . $dt1::getTabela()['nome'][0];

It doesn't even print the ---.
also is blank. What is being missed here? Tried also this but didn't work.

Answer Source

You shouldn't use $this in a static function / for static properties as there is not necessarily an object context to use.

Instead, you should use self::$tabela everywhere instead.

Or change your variable (and the related methods...) to a "normal" protected property:

protected $tabela;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download