Skip to content
Advertisement

PHP Function Arguments: Array of Objects of a Specific Class

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

JavaScript

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

JavaScript

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:

JavaScript

Thanks!

Advertisement

Answer

No, it’s not possible to do directly. You might try this “clever” trick instead:

JavaScript

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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement