Jump to content

Simple OOP question


pedrobcabral

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.
Link to comment
https://forums.phpfreaks.com/topic/20894-simple-oop-question/
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]
Link to comment
https://forums.phpfreaks.com/topic/20894-simple-oop-question/#findComment-92565
Share on other sites

Archived

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

×
×
  • Create New...

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.