Page 1 of 1
[SOLVED] Determine if $this->$variable is set
Posted: Tue Aug 09, 2005 8:29 am
by anjanesh
Im not getting this to work :
Code: Select all
// Loop
{
if (isset($this->$key))
$this->$key = $val;
}
I need it like
Code: Select all
if (isset($this->a))
$this->a = $key
if (isset($this->r))
$this->r = $key
if (isset($this->s))
$this->s = $key
if (isset($this->c))
$this->c = $key
.
.
.
so I put this in a loop.
Its the line if (isset($this->$key)) thats not working - but no errors or
warnings though.
Any ideas ?
Thanks
Posted: Tue Aug 09, 2005 8:39 am
by Grim...
Have you tried
Code: Select all
<?php
$val = $this->$key;
if ($val)
{
...
Posted: Tue Aug 09, 2005 8:43 am
by anjanesh
Well...the loop like this :
Code: Select all
while (list($key, $val) = each($arr))
{
if (isset($this->$key))
$this->$key = $val;
}
Unfortunately none of the class members gets assigned.
Posted: Tue Aug 09, 2005 8:54 am
by infolock
Code: Select all
if(!empty($this->key) || isset($this->key) || $this->key != '') {
echo $this->key;
}
if this does not work, then you are not setting the var.
Posted: Tue Aug 09, 2005 8:55 am
by anjanesh
I think Im not looking for isset($this->$key), I need to check if something like $this->$key is declared in the class. I thought isset() would do the job.
Posted: Tue Aug 09, 2005 9:08 am
by anjanesh
Any way to get this working ?
Code: Select all
class cls1
{
var $a;
// Constructor
function cls1()
{
$b = "a";
if (isset($this->$b)) // Need this to return TRUE
{
$this->$b = "somevalue";
}
}
}
}
Thanks.
[EDIT] : Added the minus (-)
Posted: Tue Aug 09, 2005 9:14 am
by feyd
missing a minus in there...
assuming you have that correct, I don't see why that wouldn't technically work..
Posted: Tue Aug 09, 2005 9:20 am
by anjanesh
Ok...This is actual scenario
Code: Select all
class cls1
{
/*
Many class data members defined
*/
// Constructor
function cls1($arr)
{
while (list($key, $val) = each($arr))
{
if (isset($this->$key))
{
$this->$key = $val;
}
}
}
}
And
if (isset($this->$key)) is always returning FALSE.
I had to do $this->$key = $val; without the conditional statement...now even non-defined members are defined ! Atleast within the constructor.
Posted: Tue Aug 09, 2005 9:46 am
by feyd
here's the thing, if the code you posted previously was it for the class, then $a isn't set in the class. Sure it's defined as a possible, but since php doesn't care about undefined properties in a class until you start using them, not to mention you can create new properties on the fly, and php will roll with it..
Posted: Tue Aug 09, 2005 9:52 am
by anjanesh
So is it possible to check if $key is just defined in the class ?
Posted: Tue Aug 09, 2005 10:00 am
by feyd
if you have php5, yes.
Code: Select all
<?php
class foo
{
var $bar;
}
echo ReflectionClass::export('foo');
?>
outputs
Code: Select all
Class [ <user> class foo ] {
@@ /var/usr/test.php 4-6
- Constants [0] {
}
- Static properties [0] {
}
- Static methods [0] {
}
- Properties [1] {
Property [ <default> public $bar ]
}
- Methods [0] {
}
}
Posted: Tue Aug 09, 2005 11:02 am
by anjanesh
Okay ! So in PHP5 it is possible ! Cool !
I have PHP5 on my PC - need to get my hosting Co to switch from 4.11 to 5.
Thanks Feyd for this new info.