Page 1 of 1

Determining the type of object

Posted: Thu Apr 24, 2008 11:37 pm
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?

Re: Determining the type of object

Posted: Fri Apr 25, 2008 12:43 am
by Christopher

Re: Determining the type of object

Posted: Fri Apr 25, 2008 1:00 am
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.

Re: Determining the type of object

Posted: Fri Apr 25, 2008 2:32 am
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.

Re: Determining the type of object

Posted: Fri Apr 25, 2008 3:03 am
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

Re: Determining the type of object

Posted: Sat Apr 26, 2008 4:55 am
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
}

Re: Determining the type of object

Posted: Sat Apr 26, 2008 11:26 am
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.

Re: Determining the type of object

Posted: Sat Apr 26, 2008 1:30 pm
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.

Re: Determining the type of object

Posted: Sat Apr 26, 2008 10:46 pm
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.

Re: Determining the type of object

Posted: Sat Apr 26, 2008 11:15 pm
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.

Re: Determining the type of object

Posted: Sun Apr 27, 2008 12:51 am
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.

Re: Determining the type of object

Posted: Sun Apr 27, 2008 1:45 am
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...

Re: Determining the type of object

Posted: Sun Apr 27, 2008 11:56 am
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.