Determining the type of object

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
mabus
Forum Newbie
Posts: 17
Joined: Wed Apr 16, 2008 11:52 pm

Determining the type of object

Post by mabus »

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?
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Determining the type of object

Post by Christopher »

(#10850)
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Determining the type of object

Post by Mordred »

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.
mabus
Forum Newbie
Posts: 17
Joined: Wed Apr 16, 2008 11:52 pm

Re: Determining the type of object

Post by mabus »

arborint wrote:The manual covers this:

http://www.php.net/manual/en/language.o ... inting.php
This doesn't really answer the question. What this manual shows is exactly what I showed in my previous example. I know for a fact that PHP will give out an error, if you have a function structure like my example, and pass something that doesnt' satisfy the requirements of its arguement. What I am interested on is something that you can pass on assert, or anywhere that might check the validity of a particular object variable. That means, it is not necessary that you'll get that error when you pass a certain data type that will not qualify to a method's attribute requirements.
Mordred wrote: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.
I haven't looked into this yet, but I will . thanks .

thanks for the tips guys.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Determining the type of object

Post by Mordred »

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Determining the type of object

Post by Chris Corbyn »

Mordred wrote:2. "if (is_a())" looks like what you want if you want to "get married" to the C++-style of OOP
But if you're coding in PHP5 under E_STRICT then you'll need the "instanceof" operator:

Code: Select all

if ($obj instanceof SomeType) {
  //whatever
}
mabus
Forum Newbie
Posts: 17
Joined: Wed Apr 16, 2008 11:52 pm

Re: Determining the type of object

Post by mabus »

Mordred wrote: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

heheheheh..its not that I wanna be married to it , but I do want to apply what I think is the correct way of implementation of OOP. Since I am using OOP as dicipline in developing my apps, then I might as well do it right.
Chris Corbyn wrote:But if you're coding in PHP5 under E_STRICT then you'll need the "instanceof" operator:
This one works, I just found out about this "instanceof", and is good enough for me.

Thanks for the help guys.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Determining the type of object

Post by Mordred »

mabus wrote: heheheheh..its not that I wanna be married to it , but I do want to apply what I think is the correct way of implementation of OOP. Since I am using OOP as dicipline in developing my apps, then I might as well do it right.
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.
mabus
Forum Newbie
Posts: 17
Joined: Wed Apr 16, 2008 11:52 pm

Re: Determining the type of object

Post by mabus »

[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.
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Determining the type of object

Post by Christopher »

mabus wrote: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.
Duck typing focuses on the interface, not the class. So it is really extending the idea of polymorphism beyond a common parent class or even a formal interface.
mabus wrote: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.
I think inheritance has taken a back seat to composition in modern OOP. And overriding and overloading are of less importance in PHP that most other languages. OO for design is fairly language independent, but OO for implementation has specific best practices in different languages.
(#10850)
mabus
Forum Newbie
Posts: 17
Joined: Wed Apr 16, 2008 11:52 pm

Re: Determining the type of object

Post by mabus »

arborint wrote:I think inheritance has taken a back seat to composition in modern OOP. And overriding and overloading are of less importance in PHP that most other languages. OO for design is fairly language independent, but OO for implementation has specific best practices in different languages.
This is absolutely true. Now, I'm beginning to think on where the line is drawn between the "old fashioned" and the "modern" OOP. It just really boils down to the language though.

I recalled reading somtehing about php's future versions will have multiple inheritance capabilties. I wonder what you guys say about that? I say it would be bad.
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Determining the type of object

Post by Christopher »

mabus wrote:I recalled reading somtehing about php's future versions will have multiple inheritance capabilties. I wonder what you guys say about that? I say it would be bad.
Agreed. :)

PHP looks similar to, but is fundamentally different than languages that are designed for long-running programs. A PHP application is design to load a small fraction of a large codebase each request. Everything is created, run and then destroyed in a fraction of second. Next request, same thing with a slightly different slice of the codebase. That does not lend itself to getting too fancy...
(#10850)
mabus
Forum Newbie
Posts: 17
Joined: Wed Apr 16, 2008 11:52 pm

Re: Determining the type of object

Post by mabus »

arborint wrote: PHP looks similar to, but is fundamentally different than languages that are designed for long-running programs. A PHP application is design to load a small fraction of a large codebase each request. Everything is created, run and then destroyed in a fraction of second. Next request, same thing with a slightly different slice of the codebase. That does not lend itself to getting too fancy...
I agree to this. Although there are times when I wish that php could be persistent too. I had to do serializing to achieve something like this. I'm not so sure if it is a good , or the best way to tackle transferring an object to another process, but it worked.
Post Reply