Array values as variables VS. Array values as "properti

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rd64pro
Forum Newbie
Posts: 7
Joined: Tue Jun 18, 2002 6:10 pm
Location: Sacramento, CA

Array values as variables VS. Array values as "properti

Post by rd64pro »

So, this should be an easy one for you guys, though it seems remarkably stupid to me...

Code: Select all

Example #1:

class Test {
    function get_month() {
        $todays_date = getdate();
        return 'This month is '. $todays_dateї'month'];
    }
}

$t = new Test;
print $t->get_month();

//output:
This month is July

Code: Select all

Example #2:

class Test {
    var $todays_date;
    function get_month() {
        $this->$todays_date = getdate();
        return 'This month is '. $this->$todays_dateї'month'];
    }
}

$t = new Test;
print $t->get_month();

//output:
This month is Array
You will note that the only difference is the use of $this, making $todays_date a "property" instead of a local variable.

Why does Example #2 not work correctly, and what approach do I need take to remedy this?

-Ryan
User avatar
untouchable
Forum Newbie
Posts: 7
Joined: Sat Jul 06, 2002 11:41 pm

it's a property not a variable

Post by untouchable »

Code: Select all

<?
class Test &#123;
    var $todays_date;
    function get_month() &#123;
        $this->todays_date = getdate();
        return 'This month is '. $this->todays_date&#1111;'month'];
    &#125;
&#125;

$t = new Test;
print $t->get_month();

$this->todays_date; //this is correct
$this->$todays_date; //this is not.
?>
I had trouble with this too...
rd64pro
Forum Newbie
Posts: 7
Joined: Tue Jun 18, 2002 6:10 pm
Location: Sacramento, CA

Post by rd64pro »

Geeze... I feel like a dumb-ass. Thanks for help, untouchable.

-Ryan
Post Reply