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 3787058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:46:52+00:00 2026-05-19T11:46:52+00:00

I am currently working on a framework and have come accros a snag… how

  • 0

I am currently working on a framework and have come accros a snag… how should I handle incorrect parameter types when someone calls a function in the framework?

Example:

// Title is expected to be string, comment_num is expected to be int
function example1($title, $comment_num) {


 // Doesnt throw error, just converts type 
 $title = (string) $title;
 $comment_num = (int) $comment_num;

}

or

// Title is expected to be string, comment_num is expected to be int

function example2($title, $comment_num) {


 if (!is_string($title)) {

  trigger_error('String expected for first parameter', E_USER_WARNING);
  return;
 }

 if (!is_string($title)) {

  trigger_error('Int expected for second parameter', E_USER_WARNING);
  return
 }
}

Or would a mixture of both work? Throw an error and convert the type anyway?

What would be the best way of doing this? I plan on releasing it so it’s not just going to be me using it, therefore I want to think of the best way for others as well. Thanks.

EDIT!!!

So I decided to give the answer but i also wanted to post the code i made which allows me to quickly check types. Its abit rough but it works well enough.

function __type_check($params) {

    if (count($params) < 1) {

        return; 
    }
    $types = func_get_args();
    array_shift($types);

    $backtrace = debug_backtrace();
    $backtrace = $backtrace[1];

    $global_types = array(
        'bool'  => 'boolean',
        'int'   => 'integer',
        'float' => 'double' 
    );

    $error = false;


    for ($i = 0, $j = count($types); $i < $j; ++$i) {

        if (strpos($types[$i], ',') === false) {

            $type = strtolower($types[$i]);

            if (isset($global_types[$type])) {

                $type = $global_types[$type];
            }

            if (gettype($params[$i]) != $type) {
                $error = true;
                break;
            }

        } else {

            $current_types = array_map('trim', explode(',', $types[$i]));

            foreach ($current_types as $type) {

                $type = strtolower($type);  

                if (isset($global_types[$type])) {

                    $type = $global_types[$type];
                }

                if (gettype($params[$i]) == $type) {

                    continue 2; 
                }
            }

            $error = true;
            break;
        }       
    }

    if ($error) {
        trigger_error($backtrace['function'] . '() expects parameter ' . ($i + 1) . ' to be ' . $types[$i] . ', ' . gettype($params[$i]) . ' given', E_USER_WARNING);
        return false;
    }

    return true;
}

And you would use it like this:

function string_manipulation($str, $str2, $offset = 1) {

    if (!__type_check(func_get_args(), 'string', 'string', 'int,float')) {

        return false;   
    }   

    // do manipulation here
}

That would basically check that the first and second parameters are strings, and the 3rd parameter is an integer or a float. You can combine any types ‘string,int,array,object’ etc and all valid types are taken from gettype

/* Known bugs */
null is a type, cant decide on if it should be or not
if you enter a class name, it doesn’t check instance of but just does typecheck
havent figured out a good way to trigger the error… meh

Thats it from me, the bugs can be easily fixed 😀

  • 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-19T11:46:53+00:00Added an answer on May 19, 2026 at 11:46 am

    It depends.

    PHP is a dynamically typed language, and sometimes for a good reason. Since it deals a lot with HTTP data, which is all strings all the time, numbers aren’t necessarily always of type int and will still work fine for general operations.

    Strictly enforcing primitive types is often very un-PHPish and could be a hassle to work with.

    The usual way to do things in PHP is to accept arguments in almost any type and work with them in good faith until you need to have specific results or the type becomes an issue.

    function example1($title, $comment_num) {
    
        // do some operations that should work error-free regardless of type
    
        if ($result != 'something specific you expect here') {
            throw new Exception('Something went wrong!');
            // or
            trigger_error('Something went wrong!');
            return false;
        }
    
        // continue with $result
    }
    

    You can go the OOP route and construct objects this way. Objects will be flexible to some degree in what they accept. If they succeed in being constructed, you have a defined object of a specific type that you can use for PHP-enforced type hinting:

    function example1(TitleObject $title) {
        // rest assured that $title is of the right type
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working with some users that do not have the .NET Framework
I am currently working on a very specialized PHP framework, which might have to
So I have an .htaccess file in a framework I am currently working on
I am currently working on a project using the Express framework. I have a
I'm currently working on a project where I use Zend Framework with Propel. I'm
I am currently working on an web application that uses ASP.NET 2.0 framework. I
I'm working on a project that is currently using the Zend Framework 1.7.6, however
Currently working in the deployment of an OFBiz based ERP, we've come to the
I'm currently working on my own PHP Framework, and I need some help figuring
I am currently working on my own version of membership using Entity Framework 4.0

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.