0

As I understand, there is no way (even in PHP 7) to force function to take typehint of its parameter as an array of objects.

I think there is a workaroud to do that by defining another object as Traversable, which would be the container for all MyObject that would be otherwise in the array and setting the typehint to that Traversable.

But it would be so cool, if I could do this:

public function foo(MyObject[] $param) {}

So my question is, is there any reason why PHP doesn't implement this?

2
  • This was proposed but rejected by an overwhelming majority.... though it may yet be re-introduced Commented Oct 19, 2015 at 10:50
  • Thank you @MarkBaker for linking the RFC. I still hold a spark of hope for this. Commented Oct 19, 2015 at 11:11

2 Answers 2

1

you can also

$arrYourObjectType = new YourObjectType[];

Then if the object array is your return type for the function, in your phpdoc, to type hint the return value, in the phpdoc above your function:

/**
* @param $whatever
* @return array ...$arrYourObjectType
**/
public function someFunction($whatever){
  $arrYourObjectType[] = new YourObjectType[];
  $x=0;
  foreach($arrValues as $value)
  {
      $objYourObjectType = new YourObjectType();
      $objYourObjectType->setSomething($value[0])
          ->setSomethingElse($value[1]);
      (and so on)
      //we had to set the first element to a new YourObjectType so the return
      //value would match the hinted return type so we need to track the 
      //index
      $arrYourObjectType[$x] = $objYourObjectType;
      $x++;
  }
  return $arrYourObjectType;
}

Then in IDE's such as php storm, when using a class containing that function the return value of the function will be treated like an array of your object (properly hinted) and the IDE will expose the object methods on each element of the object array properly.

You can do things easy/dirty without all this, but phpStorm won't hint the methods on the elements of your object array properly.

If feeding an array of YourObjectType to a function...

/**
*@param YourObjectType ...$arrYourObjectType
**/
public function someFunction(YourObjectType...$arrYourObjectType){
  foreach($arrYourObjectType as $objYourObject)
  {
    $someval = $objYourObject->getSomething();//will be properly hinted in your ide
  } 
}

It's all about the ellipses when feeding and retrieving object arrays :-)

Edit: I had a few things wrong with this because I did it from memory... corrected... Sorry bout this...

Sign up to request clarification or add additional context in comments.

Comments

0

I don't understand well your question but if you want to insert data inside a object you can do:

<?php
class Insert
{
public $myobject = array();

public function foo($insert_in_object, $other_param_in_object) {
$this->myobject[] = $insert_in_object;
$this->myobject[] = $other_param_in_object;
return $this->myobject;
}

}

$start = new Insert();
$myobject = $start->foo('dog', 'cat');
var_dump($myobject)
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.