473,836 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1573
wi***********@g mail.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*******@attgl obal.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***********@g mail.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*******@attgl obal.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***********@g mail.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_exist s('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*******@attgl obal.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_exist s() works on
objects, but someone suggested I used property_exists (), and that seems
to solve the riddle.
Psst! You can use array_key_exist s('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*******@attgl obal.net
=============== ===
Aug 9 '06 #10

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

Similar topics

5
3389
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' is not defined" Please, could you tell me what for function exist to test the variable with Python?
1
29195
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 EQUAL 'Little Red Riding Hood'"><xsl:value-of select="@title"></xsl:if> and
7
2567
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 style and false if not. Have you any idea? Thank in advance an dbest regards. Pietro.
25
3917
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 { code for netscape users } If IE-test { code for ie users
14
7710
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 alternatives? Additionally, might there be cross-platform alternatives, say in a library like Boost, or something else? -Matt
5
19539
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" state="CA" /> What's the best why of determining if the attribute zipcode exists in this node? If I try accessing the attribute with code like: ZipCode = myNode.Attributes("zipcode").Value
13
1888
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
2941
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; document.write( ); </script></body></html> ---------------------
1
2439
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 ARListView.Design {
0
9810
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9656
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10526
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5641
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5811
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.