
August 8th, 2006, 11:45 AM
| | | Test for the existence of a null property
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 | 
August 8th, 2006, 12:25 PM
| | | 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
================== | 
August 8th, 2006, 01:05 PM
| | | 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
==================
| | 
August 8th, 2006, 02:45 PM
| | | 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 | 
August 8th, 2006, 08:15 PM
| | | 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. | 
August 9th, 2006, 01:05 AM
| | | 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
================== | 
August 9th, 2006, 07:25 AM
| | | 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');
| | 
August 9th, 2006, 07:35 AM
| | | 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.
| | 
August 9th, 2006, 07:45 AM
| | | 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 | 
August 9th, 2006, 12:45 PM
| | | 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
================== |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|