Forum Moderators: coopster
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
propertyas 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;
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
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