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
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 09, 2005 8:29 am
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... » Tue Aug 09, 2005 8:39 am
Have you tried
Code: Select all
<?php
$val = $this->$key;
if ($val)
{
...
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 09, 2005 8:43 am
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.
infolock
DevNet Resident
Posts: 1708 Joined: Wed Sep 25, 2002 7:47 pm
Post
by infolock » Tue Aug 09, 2005 8:54 am
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.
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 09, 2005 8:55 am
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.
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 09, 2005 9:08 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 09, 2005 9:14 am
missing a minus in there...
assuming you have that correct, I don't see why that wouldn't technically work..
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 09, 2005 9:20 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 09, 2005 9:46 am
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..
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 09, 2005 9:52 am
So is it possible to check if $key is just defined in the class ?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 09, 2005 10:00 am
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] {
}
}
anjanesh
DevNet Resident
Posts: 1679 Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India
Post
by anjanesh » Tue Aug 09, 2005 11:02 am
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.