PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 30, 2020 12:05 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: Thu Apr 24, 2008 11:37 pm 
Offline
Forum Newbie

Joined: Wed Apr 16, 2008 11:52 pm
Posts: 17
I usually user php's "assert" function to test variables.

Now, is there a way to determine the type of object that is initialized to a variable? The function "is_object" is not good enough, 'cause it will only determine a particular variable or not, but what I want is to determine if a particular object satisfies the set requirement.

for example:

public function GetSomething(MyDesiredObject $newObject) {
... some implementation here ...
}

Now, I want this function to be able to accept only that "MyDesiredObject" data type. usually I'd do an assert from within the function to test, but how about if I want to determine if the value of that "$newObject" variable is really of "MyDesiredObject" data type?

How do we do this on PHP?


Top
 Profile  
 
PostPosted: Fri Apr 25, 2008 12:43 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
The manual covers this:

http://www.php.net/manual/en/language.o ... inting.php

_________________
(#10850)


Top
 Profile  
 
PostPosted: Fri Apr 25, 2008 1:00 am 
Offline
DevNet Resident
User avatar

Joined: Sun Sep 03, 2006 5:19 am
Posts: 1579
Location: Sofia, Bulgaria
I am a fan of "duck typing", a much more flexible solution. is_callable() is your guy.
If it's something tightly coupled to a certain class, make it a method.


Top
 Profile  
 
PostPosted: Fri Apr 25, 2008 2:32 am 
Offline
Forum Newbie

Joined: Wed Apr 16, 2008 11:52 pm
Posts: 17


Top
 Profile  
 
PostPosted: Fri Apr 25, 2008 3:03 am 
Offline
DevNet Resident
User avatar

Joined: Sun Sep 03, 2006 5:19 am
Posts: 1579
Location: Sofia, Bulgaria
1. Don't use assert(). Ever.
2. "if (is_a())" looks like what you want if you want to "get married" to the C++-style of OOP


Top
 Profile  
 
PostPosted: Sat Apr 26, 2008 4:55 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
PostPosted: Sat Apr 26, 2008 11:26 am 
Offline
Forum Newbie

Joined: Wed Apr 16, 2008 11:52 pm
Posts: 17


Top
 Profile  
 
PostPosted: Sat Apr 26, 2008 1:30 pm 
Offline
DevNet Resident
User avatar

Joined: Sun Sep 03, 2006 5:19 am
Posts: 1579
Location: Sofia, Bulgaria


Top
 Profile  
 
PostPosted: Sat Apr 26, 2008 10:46 pm 
Offline
Forum Newbie

Joined: Wed Apr 16, 2008 11:52 pm
Posts: 17
[quote="Mordred"]
There's more than one right here. Both duck typing and class checking are valid options in a weakly typed language such as PHP, duck typing allows more flexibility though.
[\quote]

True. However, I would have to say that duck typing, could be something like loose typing your objects, since it determines type compatibility only by that part of a type's structure that is accessed, not the whole structure itself. But then again, this is also one of the most important key elements in OOP, specially if we're talking about classes with inheritance, and with methods that involves overriding, and overloading.


Top
 Profile  
 
PostPosted: Sat Apr 26, 2008 11:15 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sun Apr 27, 2008 12:51 am 
Offline
Forum Newbie

Joined: Wed Apr 16, 2008 11:52 pm
Posts: 17


Top
 Profile  
 
PostPosted: Sun Apr 27, 2008 1:45 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sun Apr 27, 2008 11:56 am 
Offline
Forum Newbie

Joined: Wed Apr 16, 2008 11:52 pm
Posts: 17


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group