19

I have a function that takes a member of a particular class:

public function addPage(My_Page $page)
{
  // ...
}

I'd like to make another function that takes an array of My_Page objects:

public function addPages($pages)
{
  // ...
}

I need to ensure that each element of $pages array is an instance of My_Page. I could do that with foreach($pages as $page) and check for instance of, but can I somehow specify in the function definition that the array has to be an array of My_Page objects? Improvising, something like:

public function addPages(array(My_Page)) // I realize this is improper PHP...

Thanks!

6 Answers 6

20

No, it's not possible to do directly. You might try this "clever" trick instead:

public function addPages(array $pages)
{
    foreach($pages as $page) addPage($page);
}

public function addPage(My_Page $page)
{
    //
}

But I 'm not sure if it's worth all the trouble. It would be a good approach if addPage is useful on its own.

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

Comments

19

You can also add PhpDoc comment to get autocomplete in IDE

    /**
     * @param My_Page[] $pages
     */
    public function addPages(array $pages)
    {
      // ...
    }

2 Comments

This is helping and with VS code and the github.com/bmewburn/vscode-intelephense it's very helpful with the autocomplete :)
This should be the accepted answer in 2025.
3

From php5.6 you can use splat operator to type hint all array elements as function params. In this case you don't have more parameters than Pages stored in array so it should work fine:

public function addPages(Page ...$pages)

More info: https://www.php.net/manual/en/migration56.new-features.php

https://lornajane.net/posts/2014/php-5-6-and-the-splat-operator

1 Comment

it throws the error Argument #1 must be of type Page, array given in php 8.2
2

Instead of passing an array of objects (array(My_Page)), define your own class and require an instance of it in your function:

class MyPages { ... }

public function addPages(MyPages pages)

3 Comments

and how exactly I'm able to store an indefinite amount of specific objects in that class?
@goose I have no idea what I meant with this answer back then, sorry. This answer looks pretty useless to me
xd it's kk. but it's weird that 2 dudes upvoted it and found this useful
0

if you use the class, you can do some thing like this:

interface addPageInterface
{
   public function someThing();
}


class page implements addPageInterface
{
   public function someThing()
   {
      //for example: create a page
   }
}


class addPage
{
   public function __construct(addPageInterface $page)
   {
       //do some thing...

      return $page; //this will return just one page
   }
}


class addPages
{
   public function __construct(addPageInterface... $page)
   {
      //do some thing...

      return $page; //this will return an array which contains of page
   }
}

1 Comment

Give proper explanation with your answer with proper reasoning.
-2
<?php
        
    class Page{
            public $a;
    }
        
    
    function addPages(Page ...$pages){
        foreach($pages as $page){
        
        }
    }
        
        
    $pages = [];
    $pages[]  = new Page;
    $pages[]  = new Page;
    $pages[]  = new Page;
    addPages(...$pages);
    
?>

i think its better way

4 Comments

Explain why do you think this is a better way please.
This will throw an error: Argument #1 must be type of Page, array given.
Its not throwing any error after version 7.0 its working successfuly
it throws the error Argument #1 must be of type Page, array given in php 8.2

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.