0

I am trying to use a class within a class, but seem to be having an issue with the initialised content of the class. It can see the class structure fine if you do a var_dump, but it wont see the content you have initialised with. I know im probably missing something pretty obvious, any pointers would be great. An example is below...

class firstClass()
{

    public $thedate;

    function __construct()
    {
        $this->thedate = date();
    }

}


class secondClass()
{

    public $datefrom1stclass;

    function __construct()
    {
        $this->datefrom1stclass = new firstClass;

        echo $this->datefrom1stclass->thedate;

    }

}

Sorry if I have not explained very well, If I do a var_dump I get the following:

object(firstClass)#3 (1) { ["thedate"]=> NULL }

Any pointers would be appreciated!

2
  • 1
    Once you fix the syntax errors and pass an argument to date(), it works fine: codepad.org/kDvuEWR3 Commented Jul 9, 2012 at 11:06
  • Sorry guys, looks like I missed an 'S' out of __construct for the first class, which is why it wasnt being initialised with anything!! Commented Jul 9, 2012 at 11:16

2 Answers 2

1

You shouldn't call date() without any parameters: at least one (a format, as a string) should be given:

$this->thedate = date('D M j G:i:s T Y');

The rest of your code is correct (although I'd prefer new firstClass() form, with parentheses - it's more readable).

Sign up to request clarification or add additional context in comments.

Comments

0

Here is the correct code :) Enjoy `

public $thedate;

function __construct()
{
    $this->thedate = date("Ymd");
}

}

class secondClass {

public $datefrom1stclass;

function __construct()
{
    $this->datefrom1stclass = new firstClass;

    echo $this->datefrom1stclass->thedate;

}

} $var = new secondClass(); var_dump( $var ); ?> `

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.