473,855 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typeof in PHP?

Hi All,

How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do
this:

if($obj typeof Iterator)
...

or

if(typeof($obj) == Iterator)
...
It seems really kludgy to just try to iterate over something and wait
for it to fail, but AFAIK that's the only way to do this in PHP.

Can someone please point me in the right direction on this? Many thanks
in advance.

Cheers,
Joshua Frank
*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 20 '06 #1
7 43623
On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
<jf****@archime trics.remove-luncheon-meat.com> wrote:
Hi All,

How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do


You really didn't look hard enough!

In PHP4, use the is_a() function.
In PHP4, use the instanceof operator.

Apr 20 '06 #2
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these terms.
I did try, really.

Thanks again.

Wayne wrote:
On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
<jf****@archime trics.remove-luncheon-meat.com> wrote:
Hi All,

How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do


You really didn't look hard enough!

In PHP4, use the is_a() function.
In PHP4, use the instanceof operator.

*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 20 '06 #3
You may also find the following helpful:

http://us3.php.net/manual/en/ref.classobj.php (class/object functions)
http://us3.php.net/manual/en/languag...reflection.php (Reflection
API)

Also note, in PHP4, classes have lower case names when you use
get_class(), even if defined with upper case letters. ie: MyClass =>
myclass.
In PHP5, it stays the same as declared.

Typo by Wayne: instanceof is available in PHP5, not PHP4.

Also be aware of differences between how objects are handled in PHP5 vs
PHP4.
PHP5 treats them more like java, they are all passed by reference.
PHP4 copies them all over the place (thus the use for $o = & new
Object() );
Search around in the manual and user comments for more specific and
detailed information.

-Richard Levasseur

Joshua Frank wrote:
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these terms.
I did try, really.

Thanks again.

Wayne wrote:
On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank
<jf****@archime trics.remove-luncheon-meat.com> wrote:
Hi All,

How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do


You really didn't look hard enough!

In PHP4, use the is_a() function.
In PHP4, use the instanceof operator.

*** Posted via a free Usenet account from http://www.teranews.com ***


Apr 20 '06 #4
On Thu, 20 Apr 2006 12:57:48 -0400, Joshua Frank wrote:
How can you tell if an object supports an interface? I've got a
situation where I need to tell if an object implements Iterator, but
there doesn't seem to be any operator like typeof that would let you do
this:

if($obj typeof Iterator)
...


There is gettype function.

--
http://www.mgogala.com

Apr 21 '06 #5
On Thu, 20 Apr 2006 13:12:49 -0400, Joshua Frank
<jf****@archime trics.remove-luncheon-meat.com> wrote:
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these terms.
I did try, really.


Googling is not too useful for something like this. Your best bet is
to download the CHM manual from php.net (if on Windows) and just look
around in it. For example, as others have pointed out, it has a whole
section on classes and objects.

In otherwords, RTFM! ;)

Later,

Apr 21 '06 #6
Joshua Frank wrote:
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these terms.
I did try, really.


Instead of googling or trying, how about www.php.net -> documentation
-> <select language> -> Function reference -> Classes/Objects.

;-)

/Marcin
Apr 25 '06 #7
I actually tried googling with the site:php.net qualifier and still
didn't find this. So I have to say that at least this particular bit of
info was not readily googled. Now that I know what the syntax is, it's
easy to find, but without knowing in advance, the best I could think of
was to type in the relevant keywords from other languages, like typeof,
and that did not find anything for me.

Marcin Dobrucki wrote:
Joshua Frank wrote:
Thanks. That did the trick. Incidentally, I spent a while googling
this without finding this info. I tried quite a few combinations of
words like type, interface, typeof, etc... without finding these
terms. I did try, really.


Instead of googling or trying, how about www.php.net -> documentation
-> <select language> -> Function reference -> Classes/Objects.

;-)

/Marcin

*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 26 '06 #8

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

Similar topics

4
299
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in them and I need to know what kind of item a user clicked on so I can use it for drag and drop
4
536
by: ichor | last post by:
hi what is the use of the typeof keyword , and how does it differ from the GetType method. i found the GetType method useful but i fail to understand the use of the typeof method. i have tried out a very simple example which i made up myself. Employee e = new Employee(); ContractEmp ec =new ContractEmp(); MessageBox.Show(typeof(Employee).ToString());
7
9987
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
3
950
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
1
1642
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' ' Never Enters here.... ' End If
11
5628
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
2347
by: Andrew Robinson | last post by:
I am guessing there is a simple solution but given a type T, how can I check for nullability? how can I accomplish the following? bool nullable = typeof(int).IsNullable; // false bool nullable = typeof(int?).IsNullable; // true bool nullable = typeof(string).IsNullable; // true Thanks for any help.
4
7116
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the controls on the form. Snippet of the coding: <..snipped..> ElseIf TypeOf oCtrl Is CheckBox Then oCtrl.Value = False <..snipped..> ElseIf TypeOf oCtrl Is OptionGroup Then
20
9326
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues = displayValuesArray.length; for (i=0; i<nDisplayValues; i++) {
20
21798
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof() macro, but the same code is not getting compiled in solaris forte compiler and in microsoft VS 2003 compiler. I tried something like below:
0
9903
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
9754
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
11044
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10767
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
7927
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
7084
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4567
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
4168
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.