Jump to content

Recommended Posts

Hi guys,

I am a newbie in PHP, but experienced in other OOP languages, so sorry if my questions are maybe trivial for some of the forum members, but hope can get some answers for my questions.

Tried to google some info on the fact if PHP supports object casting, but could not find any useful answer, except one, but with a poor description.So is it possible in PHP to use object casting (I do not mean vasriable casting, like boolean, integer types)?

My example:

I define a class called Test with some logic, then create some objects of Test type. Then let's say I create an array of Test objects, so I can access any of the Test objects from the array by the array index. But I want to make sure that the returned array member for xy index is actually a type of Test. The logic above is maybe trivial, but I just tried to simplify what I want to achieve.

So now, how can I achieve this type of casting in PHP?The pseudocode is below:

//$ArrayList is an array of obejcts of Test type

//ArrayMember is an object, but want to make sure that it is actually of type of Test, so need an explicit casting

$ArrayMember = (Test)$ArrayList[xy];

 

I know that I could use the function get_class() for the array members to test whether they are of Test type, but I need to use casting rather than testing it.

Thank you

Ben

Link to comment
https://forums.phpfreaks.com/topic/82493-object-casting-in-php/
Share on other sites

Hi thorpe,

Thanks for the clarification, the manual is just so big I did not get to that part of the manual. Thena I supposed to use the type checking if I want to make sure the object type.

Now I've seen in the manual the is_a() function which is deprecated in PHP5.I need to write a php code backwards compatible with php4 and php5 so it makes me thinking what other way would be possible to enforce the object type checking.

There is anotehr function get_class() but that is returning the objects class name as string and it would be a bit cumbersome to always compare the classname in a string...but probably that is the only way then for both v4 and v5.

Please let me know if I got it wrong.

Thanks

 

Ben

Link to comment
https://forums.phpfreaks.com/topic/82493-object-casting-in-php/#findComment-419389
Share on other sites

Maybe this will help.

 

<?php

  class foo {}
  
  $obj = new foo();

  if (function_exists('is_a')) {
    if (is_a($obj,'foo')) {
      echo '$obj is of type foo';
    }
  } else {
    if ($obj instanceof foo) {
      echo '$obj is of type foo';
    }
  }

?>

 

is_a() is still available within php5, but seeing as its depricated is likely to be removed in php6.

Link to comment
https://forums.phpfreaks.com/topic/82493-object-casting-in-php/#findComment-419423
Share on other sites

Yeah this was a significant help thorpe, thank you.

As I need to do such a check at some places, just wrote a static function to do the check. However it is raising a question in my head, that the two functions handle the checking in two different ways and that is:

1. is_a passing a string form of the class name

2. whereas the instanceof operator excpect the class itself;

 

I am not sure if you can pass a string classname into the instanceof operator.

My function look like this:

function IsInstanceOf($object, $className)

{

//the same logic as you wrote before, except that the instanecof operator changed into:

//if( $object instanceof $className)

}

 

So is possible to solve this problem within such a function?

Link to comment
https://forums.phpfreaks.com/topic/82493-object-casting-in-php/#findComment-419485
Share on other sites

Just to add to my last respond:

I created the function to check the type of the object, where I am passing the Classname parameter as the Class itself.

Then in the function itself for the function_exists('is_a') is true, casting the $classname parameter into a string and then calling the is_a php function with the casted string parameter.

ben

Link to comment
https://forums.phpfreaks.com/topic/82493-object-casting-in-php/#findComment-419498
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.