Connecting Tech Pros Worldwide Help | Site Map

Test for the existence of a null property

  #1  
Old August 8th, 2006, 11:45 AM
wildernesscat@gmail.com
Guest
 
Posts: n/a
Hello there,

I'm looking for a method to test, whether an object has a certain
property.
Consider the following snippet:

class A { var $aaa; }
$var = new A;

(Assuming that the structure of class A is unknown) I need a way to
check whether $var->aaa exists (test positive), and whether $var->xxx
exists (test negative). I tried boolean tests, isset(), is_null(), but
they can't tell the difference.
I guess I could convert the object to an array and test its indices,
but that wouldn't be practical for large object with many fields.

Any other ideas?

Thanks in advance,
Danny

  #2  
Old August 8th, 2006, 12:25 PM
Jerry Stuckle
Guest
 
Posts: n/a

re: Test for the existence of a null property


wildernesscat@gmail.com wrote:
Quote:
Hello there,
>
I'm looking for a method to test, whether an object has a certain
property.
Consider the following snippet:
>
class A { var $aaa; }
$var = new A;
>
(Assuming that the structure of class A is unknown) I need a way to
check whether $var->aaa exists (test positive), and whether $var->xxx
exists (test negative). I tried boolean tests, isset(), is_null(), but
they can't tell the difference.
I guess I could convert the object to an array and test its indices,
but that wouldn't be practical for large object with many fields.
>
Any other ideas?
>
Thanks in advance,
Danny
>
Danny,

Once of the concepts of OO programming is encapsulation (not fully
implemented in PHP), in which can't access $aaa. Anything outside of
the class should not have access to the internal variables of the class;
nor should they concern themselves with the internals of the class.
This limits the effects of changes to the class.

Exactly what problem are you trying to resolve?


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  #3  
Old August 8th, 2006, 01:05 PM
wildernesscat
Guest
 
Posts: n/a

re: Test for the existence of a null property


Hi Jerry,

I'm trying to write a function that checks whether a given object is of
a known class (named 'A' in this example). I want it to work even if
the object has been constructed manually, starting from 'new
StdClass()'.

Danny


Jerry Stuckle wrote:
Quote:
wildernesscat@gmail.com wrote:
Quote:
Hello there,

I'm looking for a method to test, whether an object has a certain
property.
Consider the following snippet:

class A { var $aaa; }
$var = new A;

(Assuming that the structure of class A is unknown) I need a way to
check whether $var->aaa exists (test positive), and whether $var->xxx
exists (test negative). I tried boolean tests, isset(), is_null(), but
they can't tell the difference.
I guess I could convert the object to an array and test its indices,
but that wouldn't be practical for large object with many fields.

Any other ideas?

Thanks in advance,
Danny
>
Danny,
>
Once of the concepts of OO programming is encapsulation (not fully
implemented in PHP), in which can't access $aaa. Anything outside of
the class should not have access to the internal variables of the class;
nor should they concern themselves with the internals of the class.
This limits the effects of changes to the class.
>
Exactly what problem are you trying to resolve?
>
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  #4  
Old August 8th, 2006, 02:45 PM
Marcin Dobrucki
Guest
 
Posts: n/a

re: Test for the existence of a null property


wildernesscat wrote:
Quote:
Hi Jerry,
>
I'm trying to write a function that checks whether a given object is of
a known class (named 'A' in this example). I want it to work even if
the object has been constructed manually, starting from 'new
StdClass()'.
I think this is VERY different from what you asked in the first mail.
But to do that, you want something like this:

class Foo (
function Foo() {}
}

$f = new Foo();
....
if (is_a($f, 'Foo')) {
echo "yes";
}
....
echo "\$f is of type: " . get_class($f);

To do what you asked before (and hence break some main concepts in
OO), you can:

$vars = get_class_vars('Foo');

/M
  #5  
Old August 8th, 2006, 08:15 PM
Chung Leong
Guest
 
Posts: n/a

re: Test for the existence of a null property


wildernesscat@gmail.com wrote:
Quote:
Hello there,
>
I'm looking for a method to test, whether an object has a certain
property.
Consider the following snippet:
>
class A { var $aaa; }
$var = new A;
>
(Assuming that the structure of class A is unknown) I need a way to
check whether $var->aaa exists (test positive), and whether $var->xxx
exists (test negative). I tried boolean tests, isset(), is_null(), but
they can't tell the difference.
I guess I could convert the object to an array and test its indices,
but that wouldn't be practical for large object with many fields.
>
Any other ideas?
Psst! You can use array_key_exists('aaa', $var) to test for the
existence of a property. I didn't tell you that by the way.

  #6  
Old August 9th, 2006, 01:05 AM
Jerry Stuckle
Guest
 
Posts: n/a

re: Test for the existence of a null property


wildernesscat wrote:
Quote:
Hi Jerry,
>
I'm trying to write a function that checks whether a given object is of
a known class (named 'A' in this example). I want it to work even if
the object has been constructed manually, starting from 'new
StdClass()'.
>
Danny
>
>
Yes, that's a lot different than your original question.

You can use is_a() as Marcin suggested. But what's to stop me from
creating my own class 'A'?

The real question is - why do you need this "feature"? It violates
several principles of OO.

OO principles include that you don't care WHAT the object is - all you
care about is its interface (functions). If you need to depend on a
specific class, you need to rethink your design.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  #7  
Old August 9th, 2006, 07:25 AM
wildernesscat
Guest
 
Posts: n/a

re: Test for the existence of a null property


Hi Marcin,
Quote:
>From my experience, the is_a() function does not work unless the object
has been created explicitly using its class name (Foo in your example).
If the object has been created via StdClass, is_a() doesn't recognize
it.

As for using get_class_vars(), I'm getting into O(n2) here. You
suggested that I scan the properties of one object and compare them to
the other one, right? I was looking for a O(n) solution.

Regards,
Danny
Quote:
I think this is VERY different from what you asked in the first mail.
But to do that, you want something like this:
>
class Foo (
function Foo() {}
}
>
$f = new Foo();
...
if (is_a($f, 'Foo')) {
echo "yes";
}
...
echo "\$f is of type: " . get_class($f);
>
To do what you asked before (and hence break some main concepts in
OO), you can:
>
$vars = get_class_vars('Foo');
  #8  
Old August 9th, 2006, 07:35 AM
wildernesscat
Guest
 
Posts: n/a

re: Test for the existence of a null property


That's interesting. I didn't know that array_key_exists() works on
objects, but someone suggested I used property_exists(), and that seems
to solve the riddle.
Quote:
Psst! You can use array_key_exists('aaa', $var) to test for the
existence of a property. I didn't tell you that by the way.
  #9  
Old August 9th, 2006, 07:45 AM
wildernesscat
Guest
 
Posts: n/a

re: Test for the existence of a null property


Hi Jerry,
Quote:
You can use is_a() as Marcin suggested. But what's to stop me from
creating my own class 'A'?
As I told Marcin, is_a() will not be of much use here. I need an
in-depth comparison, and not just resemblance in names.
Quote:
The real question is - why do you need this "feature"? It violates
several principles of OO.
Actually, I don't think that I violate any major OO principles. This is
almost like asking an object whether it supports certain interfaces.
I'll pinpoint my question. I want to know whether a certain object has
the _public_ properties of a given class. Does that make sense? Suppose
I have an object that has public properties $aaa, $bbb, and $ccc - I'd
like to know whether class A also has them.
Quote:
OO principles include that you don't care WHAT the object is - all you
care about is its interface (functions). If you need to depend on a
specific class, you need to rethink your design.
>
Regards,
Danny

  #10  
Old August 9th, 2006, 12:45 PM
Jerry Stuckle
Guest
 
Posts: n/a

re: Test for the existence of a null property


wildernesscat wrote:
Quote:
Hi Jerry,
>
>
Quote:
>>You can use is_a() as Marcin suggested. But what's to stop me from
>>creating my own class 'A'?
>
>
As I told Marcin, is_a() will not be of much use here. I need an
in-depth comparison, and not just resemblance in names.
>
>
Quote:
>>The real question is - why do you need this "feature"? It violates
>>several principles of OO.
>
>
Actually, I don't think that I violate any major OO principles. This is
almost like asking an object whether it supports certain interfaces.
I'll pinpoint my question. I want to know whether a certain object has
the _public_ properties of a given class. Does that make sense? Suppose
I have an object that has public properties $aaa, $bbb, and $ccc - I'd
like to know whether class A also has them.
>
>
Quote:
>>OO principles include that you don't care WHAT the object is - all you
>>care about is its interface (functions). If you need to depend on a
>>specific class, you need to rethink your design.
>>
>
>
Regards,
Danny
>
Danny,

Again, that's a completely different question.

No, there's nothing wrong to determine of a class supports specific
properties. But that's not the same as expecting a specific class.

And in many cases classes shouldn't have public properties - they should
have private properties and public access methods. It isolates the
interface from the implementation.

I see a lot of public properties in PHP code - mainly because before
PHP5 you couldn't define private properties. However, if you look at
C++, Java and Smalltalk, you'll find almost no public properties - only
methods.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML DOM/Null property problem (or just VBScript related(?!)) Jon L answers 3 January 13th, 2007 11:55 PM
Q: Automatically Changing Background Color of a Table Cell? Arthur Shapiro answers 31 July 23rd, 2005 05:18 PM
does scope allow the transfer of non-global variables between functions? lawrence answers 8 July 23rd, 2005 02:37 PM
Self contained system for color transition Richard A. DeVenezia answers 3 July 20th, 2005 11:23 AM