Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5999791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:36:07+00:00 2026-05-23T00:36:07+00:00

Quick one: Is there any way to enforce types for variadic functions in PHP?

  • 0

Quick one:

Is there any way to enforce types for variadic functions in PHP? I’m assuming not, however maybe I’ve missed something.

As of now, I’m just forcing a single required argument of the needed type, and iterating to check the rest.

public function myFunction(MyClass $object){
    foreach(func_get_args() as $object){
        if(!($object instanceof MyClass)){
            // throw exception or something
        }
        $this->_objects[] = $object;
    }
}

Any better solutions?


Purpose:

A container object that acts as an iterated list of the child objects, with some utility functions. calling it with a variadic constructor would be something like this:

// returned directly from include
return new MyParent(
    new MyChild($params),
    new MyChild($params),
    new MyChild($params)
);

The other option could be an addChild method chain:

$parent = new MyParent;
return $parent
    ->addChild(new MyChild($params))
    ->addChild(new MyChild($params))
    ->addChild(new MyChild($params));

The children take several arguments to their constructor as well, so I’m trying to balance between legibility and processing expense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:36:08+00:00Added an answer on May 23, 2026 at 12:36 am

    Well I would say it depends on the number of arguments 🙂 There is nothing like a list (all arguments 1-n as MyClass [before PHP 5.6, for PHP 5.6+ see Variadic functions]), it’s more that you need to write each argument (as in your question) and it’s allowed to send more but only the ones defined will be checked.

    However you can define more and make them optional. Hence why I just wrote it depends on the number of arguments:

    public function myFunction(MyClass $object, MyClass $object2=null, MyClass $object3=null, MyClass $object4=null, ...){
        foreach(func_get_args() as $object){
            if(null === $object){
                // throw exception or something
            }
            $this->_objects[] = $object;
        }
    }
    

    With such an example, PHP would have thrown the exception already when the argument is not NULL and not MyClass (Passing NULL if given as default value, is possible to pass as well). Optional parameter should not be part of the return value of func_get_args(). I mean, if you don’t pass the third argument (here named $object3) it won’t appear in the array returned by func_get_args().

    No idea if that is more practicable for you than your the code in the question.

    If you face more such situations in your code, you can create some sort of helper to validate the input of function calls and delegate to throw the exceptions with nice error message from the helper. That will prevent duplicate code and will give more comfort in development as you can make the exception notices nicer as you write them for multiple cases.


    According to your new feedback, if you want first of all the interpreter let the work of checking, a little iteration and the addMember function would do it:

    class MyParent {
        private $children = array();
        public function __construct() {
            foreach(func_get_args() as $object) $this->addChild($object);
        }
        public function addChild(MyChild $object) {
            $this->children[] = $object;
        }
    }
    

    Instantiating the object with a wrong type of object in any of the list will prevent the Collection to be instantiated.

    you can then just do this:

    // returned directly from include
    return new MyParent(
        new MyChild($params),
        new MyChild($params),
        new MyChild($params)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick one, but thought I'd ask. Is there a better way of getting the
Is there a quick one-liner to call datepart in Sql Server and get back
Is there a quick tcpdump one-liner to print out a TCP stream that matches
What's a quick-and-dirty way to make sure that only one instance of a shell
Is there any kind of function or quick process for comparing two arrays in
Hopefully this is a really quick one ;) I have written a lexer /
I'm currently writing a quick solution for Euler Problem #4 where one must find
Is there any good empirical data on the cost of context switching between threads
Quick question; I replaced a .css file which I was referencing in html/php that
Quick question. What do you think, I have a few sites that use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.