problem with class properties not holding their value

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
RestoSpeed
Forum Newbie
Posts: 2
Joined: Tue May 29, 2007 8:15 am

problem with class properties not holding their value

Post by RestoSpeed »

I'm stuck on this one, and I don't know why, maybe it has something to do with PHP 5 (I've written for 4 before).

when I create an object and set the value of a particular class property, it changes all the properties. I'm sure it's something simple, but I just don't get it.

Code: Select all

//---CREATE PROJECT-------
    class pObject{
      var $name;
      var $start_date;
      var $summary;
    }
      
    $test_pro = new pObject;                  //---- create the object
    $test_pro->$name = "test name";    //----- set the project name property
    echo $test_pro->$name;                   //----- returns "test-name"
    $test_pro->$start_date = "date";     //----- set the project start date property
    echo $test_pro->$start_date;           //----- returns "date"
    echo $test_pro->$name;                   //----PROBLEM also returns "date"
    echo $test_pro->$summary;             //----PROBLEM 2 this also returns "date" (as upposed to NULL)
thanks in advance.
-Brandon
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$foo->$bar should be $foo->bar.

Also, "var" is deprecated. It should be "public" in this case.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ditch the dollar signs after ->, otherwise strange things can happen:

Code: Select all

class testclass {
var $var;
}
$var = 'hello';
$object = new testclass;
echo $object->$var; // tries to echo $object->hello, but fails since no such variable exists
// correct usage:
$object->var = 'object var';
echo $object->var; // outputs 'object var'
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

... I'm not sure, but probably because that's not technically a class, it's more of a structure (I learned OO as a C++ programmer so.. bare with me).

Structure's are function-less. I'm not sure hoe PHP handles them. Try giving it a default constructor.

Code: Select all

class pObject{
      var $name;
      var $start_date;
      var $summary;
      
      function __construct(){}
    }
And declare the object using that.

Code: Select all

$test_pro = new pObject();

Edit: Never mind. I was too slow. And blind. :-p
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

lol - 3 replies in 3 minutes!

We're nothing if not thorough, RestoSpeed!
RestoSpeed
Forum Newbie
Posts: 2
Joined: Tue May 29, 2007 8:15 am

Post by RestoSpeed »

Thanks guys, it was the dollar signs after the "->" I knew it was something simple. Anywhoo, as far as being a class, it will have functions, but I thought better than to post the whole def.

-Brandon
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

RestoSpeed wrote:Thanks guys, it was the dollar signs after the "->" I knew it was something simple. Anywhoo, as far as being a class, it will have functions, but I thought better than to post the whole def.

-Brandon
Good job. :-p
Personally, I hate to see long snippets.
Post Reply