Forum Moderators: coopster

Message Too Old, No Replies

OOP PHP print object variable

notice undefined property

         

MrWhippy

4:21 pm on Mar 3, 2009 (gmt 0)

10+ Year Member



Hi all,

I am trying to print part of an object I have tried to do it in two main ways. one with a function inside of the class the other "echo" of the variable using $class->variable

I keep getting PHP notice property undefined class::function
for the first method of trying. the second is not giving an error on the logs but is still not printing anything.

Is this likely to mean that my __construct is not working. i.e. there is nothing in the class to allow the variable to print?

as every example I see uses one of these two methods.

Oh and I have tried using echo and print as well.

thanks

MrWhippy

4:29 pm on Mar 3, 2009 (gmt 0)

10+ Year Member



K I just realised I could check if the issue was with my __construct by implicitley(think that is the right word in the right context) setting the variable and now it prints.

So my __construct is not working for some reason, ill have a look at that i may well be back.

coopster

4:46 pm on Mar 3, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A variable (or
property
as it is often called within a class) that is defined in the class with no value defaults to a NULL value because it is not truly set yet. Example:
class foo { 
public $bar;
function __construct()
{
if (isset($this->bar)) {
print "isset = true and the value is: ";
} else {
print "isset = false and the value is: "; // prints this one
}
var_dump($this->bar);
}
}
$foo = new foo();
exit;

MrWhippy

4:54 pm on Mar 3, 2009 (gmt 0)

10+ Year Member



Thanks for the tip about property as opposed to variable Im pretty new to this OOP stuff. I like it though.

what I cant understand at the moment is that my __construct is not working, I am passing seven variables in and receiving seven variables in the construct as I would with a normal function.

then I set using $this->property = $variable (passed in through calling construct using "new"

eg.

$test = new object($1, $2, $3,.....$7);

is how I am calling the construct and the construct does this.

public function __contstruct($one, $two, $three,....$seven)
{
echo "creating new object";
$this->one = $1;
$this->two = $2;
$this->three = $3;
$this->seven = $7;
}

I put the echo in for testing purposes and am getting no response from that either will test that with a <br> put in it.

Hope that all makes sense, used figures and numbers to show that variables and proerties have different names

milocold

5:41 pm on Mar 3, 2009 (gmt 0)

10+ Year Member



Howdy,

Just a heads up, variables can't start with a number, only letters and underscores. I'm pretty sure you knew that and only used numbers in the posted example. =)

Perhaps this can help ya:

class B{
public $a, $b, $c = '';

public function __construct($a, $b, $c){
echo 'test : ';
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
}

$obj = new B(1,2,3);
var_dump($obj);

OUTPUTS:

test : object(B)#1 (3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) }

-M. Cold

MrWhippy

6:28 pm on Mar 3, 2009 (gmt 0)

10+ Year Member



Im rushing out now, so will have a proper look at this when I get back, but yes numbers were used for examples as opposed to actual fields i am using.

back around 10:30 ish Uk time will have a look then thanks

MrWhippy

8:52 am on Mar 4, 2009 (gmt 0)

10+ Year Member



Fixed the problem I must have been really tired last night

I spealt construct wrong

contstruct

sorry for the trouble all