watkins1179 watkins1179 - 25 days ago 7
PHP Question

Php not displaying output correctly

I am having trouble with displaying my property using this code in php using php7.0. When I run php (filename) in command line it is only displaying NULL am I doing something wrong or is something funky going on?

<?php

class Task{


public $description;

public function __const($description)
{

$this->description = $description;

}

}

$task = new Task('display');

var_dump($task->description);

?>

Answer

Constructors in PHP are __construct, not __const. So either rename your method:

public function __construct($description)
{
    $this->description = $description;    
}

... or call __const:

$task = new Task();
$task->__const('display');

var_dump($task->description);
Comments