PHP Class problem

Hi,
I’m pretty new to OOP so will make this quick.

I have:


class NewDay extends DogHouse{
	private $str = date('Y-m-d H:i:s O');

which gives me:
Parse error: syntax error, unexpected ‘(’, expecting ‘,’ or ‘;’ in class-newday.php on line 1

Any ideas why?

Cheers,
Rhys

Can’t initialize a variable using a function.


class NewDay extends DogHouse{
    private $str;
    public function __construct()
    {
        $this->str = date('Y-m-d H:i:s O');  

Thanks, that’s what I thought looking at examples. Working great now.

Use magic methods


<?php

class DogHouse
{
}

class NewDay extends DogHouse
{
	private $data = array();

    public function __set($name, $value)
    {
		$this->data[$name] = $value;
	}

	public function __get($name)
	{
		if (array_key_exists($name, $this->data))
		{
            return $this->data[$name];
        }
	}
}

$obj = new NewDay;
echo $obj->a = date("Y-m-d H:i:s O");