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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:12:47+00:00 2026-06-15T13:12:47+00:00

My current way: class A { public function function_b($myint) { if (!is_numeric($myint)) return false;

  • 0

My current way:

class A {
    public function function_b($myint) {
        if (!is_numeric($myint)) return false;

        // code ...
    }
}

I would like to abandon the function is_numeric() like this:

public function function_b(Integer $myint) {
    // code ...
}

It works with arrays like this:

public function function_c(Array $arr) {
    // only executes following code if $arr is an array / instance of Array!
}

Note: the function has to return false if the value isn’t a number (int)! I don’t want to cast it.

How would you short my current code? Thanks in advance!

  • 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-06-15T13:12:47+00:00Added an answer on June 15, 2026 at 1:12 pm

    You can’t force strict types in function prototypes in PHP inherently, because it’s not a strictly typed language. PHP is a weakly typed language and trying to go against the grain will only hurt you in many situations. Also, is_numeric does not guarantee that your value is of type int (for what it’s worth).

    What you can do is analyze your need for why you think this approach is necessary in the first place and decide on how to best implement this without creating potential for bugs.

    For example, take the following scenario where what your method expects is an ID for a database query.

    class MyClass {
        public function getUser($id) {
            if (!is_int($id)) {
                throw new Exception("Invalid argument supplied. Expecting (int), but argument is of type (" . gettype($id) . ").");
            }
            // Otherwise continue
            $db = new PDO($dsn);
            $stmt = $db->prepare("SELECT username FROM users WHERE user_id = ?");
            $stmt->execute(array($id));
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $result;
        }
    }
    
    $MyObject = new MyClass;
    $result = $MyObject->getUser($_POST['id']);
    /* The problem here is $_POST will always be of type string. */
    

    What this should tell you is that it makes no sense to force type checking here since PHP will have done the right thing for you had you just let it alone.

    The question you need to be asking yourself is not “How do I force strict typing?“, but rather “Why would I need to force strict typing at all?“.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example this class: class A{ public function __call($func, $args){ if($func == 'something') call_user_func_array($this->_some_magic,
Whats the best way to get the current time (HH:MM:SS) using Objective-C. I would
Is there a simple way to insert the current time (like TIME: [2012-07-02 Mon
Using Delphi 2010, let's say I've got a class declared like this: TMyList =
I have a base class that I want to look like this: class B
I have a simple class called Job with a public ctor and public function
My controller has... class Controller_Staff extends Controller { public function before() { parent::before(); $id
Here's my current set-up: Public Interface IDatabase Function CreateParameter(name as String, dbType as <dbTypeEnumeration>)
<?php class User { private $id; private $username; public function __construct($id = null) {
After discovering on superuser that there is no current way to add syntax highlighting

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.