Jump to content

Archived

This topic is now archived and is closed to further replies.

pedrobcabral

Simple OOP question

Recommended Posts

The following does work:
[code]
$day = date('d');
echo $day;
[/code]
The following does not work:
[code]
class day {
var $day = date('d');

function display() {
echo $this->day;
}
}

$speak = new day;
$speak->display();
[/code]

Why the second does not work? The problem it's on var $day = date('d'); but i can't figure out what.
Thank you.

Share this post


Link to post
Share on other sites
Because the only variables you can define using [tt]var[/tt] is arrays. Others have to be defined in a function. Example: [code]
<?php
class day
{
var $day;

function day()
{
$this->day = date('d');
}

function display()
{
echo $this->day;
}
}

$speak = new day();
$speak->display();
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.