473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Aug 8 '06 #1
9 1559
wi***********@gmail.com wrote:
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.
js*******@attglobal.net
==================
Aug 8 '06 #2
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:
wi***********@gmail.com wrote:
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.
js*******@attglobal.net
==================
Aug 8 '06 #3
wildernesscat wrote:
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
Aug 8 '06 #4
wi***********@gmail.com wrote:
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.

Aug 8 '06 #5
wildernesscat wrote:
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.
js*******@attglobal.net
==================
Aug 9 '06 #6
Hi Marcin,
>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
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');
Aug 9 '06 #7
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.
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.
Aug 9 '06 #8
Hi Jerry,
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.
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.
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

Aug 9 '06 #9
wildernesscat wrote:
Hi Jerry,

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

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

>>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.
js*******@attglobal.net
==================
Aug 9 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Thierry S. | last post by:
Hello. I would to test the existence of a variable before to use it (like isset($myVar) in PHP). I try using "if myVar: ", but there is the error meesage (naturally): "NameError: name 'myVar'...
1
by: Porthos | last post by:
Are there 'Null' and 'not equal to' operator that I can use in xsl:if statements? I assume that there must be, but I can't figure out the syntax. For example: <xsl:if test="@title DOES NOT...
7
by: Pietro | last post by:
Hi at all, I am looking for a mean to test if a function work with a certain Browser or not. I'ld like to make a funcrion that return true if the browse is compatible with a certain funcrion or...
25
by: Treetop | last post by:
I have seen some codes that can test for the browser and give values accordingly. I tried to read the FAQ, but was unable to find a simple version of this. What I want is If Netscape-test {...
14
by: Matt | last post by:
Hello, I see other references in this newsgroup saying that the only standard C++ way to test for file existence is some variant of my code below; can someone please confirm...or offer...
5
by: Richard L Rosenheim | last post by:
What's the proper technique for checking for the existence of an attribute within a node? Lets say I did a SelectSingleNode which returned this element: <AnAddress city="San Francisco"...
13
by: DC Gringo | last post by:
How do I test for existence of a file in the file system: If FileExists(myVariable & ".pdf") = True pnlMyPanel.Visible = True End If -- _____ DC G
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
1
by: --== Alain ==-- | last post by:
Hi, I have a huge problem... My property does not appear in the "propertyGrid" of "test Container", when i test my custom control. Here is the custom control code : namespace...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.