Tuesday, February 14, 2012

PHP passing variable in __construct of a class


ive browsed many articles here but I just can't make this work for the life of me.




class Database{

private $host;

function __construct( $vhost = ''){
$this->host = $vhost == '' ? db_host : $vhost;
}

function connect(){
echo "Host: ".$this->host;
}
}



I've omitted some unnecessary code but it's same concept. I get "Fatal error: Using $this when not in object context" when trying to echo. What am I missing here?

1 comment:

  1. You probably call Database::connect() instead of $somevar->connect(). You can't refer to this in static context indeed.

    ReplyDelete