[SOLVED] Determine if $this->$variable is set

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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] Determine if $this->$variable is set

Post 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
Last edited by anjanesh on Tue Aug 09, 2005 11:03 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Have you tried

Code: Select all

<?php

$val = $this->$key;

if ($val)
{
    ...
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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 (-)
Last edited by anjanesh on Tue Aug 09, 2005 9:17 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

missing a minus in there... ;)

assuming you have that correct, I don't see why that wouldn't technically work..
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

So is it possible to check if $key is just defined in the class ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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] {
  }
}
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
Post Reply