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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:45:13+00:00 2026-05-16T23:45:13+00:00

Is there a way to tell PHP how to convert your objects to ints?

  • 0

Is there a way to tell PHP how to convert your objects to ints? Ideally it would look something like

class ExampleClass
{
    ...

    public function __toString()
    {
        return $this->getName();
    }

    public function __toInt()
    {
        return $this->getId();
    }
}

I realize it’s not supported in this exact form, but is there an easy (not-so-hacky) workaround?

———————- EDIT EDIT EDIT —————————–

Thanks everybody! The main reason I’m looking into this is I’d like to make some classes (form generators, menu classes etc) use objects instead of arrays(uniqueId => description). This is easy enough if you decide they should work only with those objects, or only with objects that extend some kind of generic object superclass.

But I’m trying to see if there’s a middle road: ideally my framework classes could accept either integer-string pairs, or objects with getId() and getDescription() methods. Because this is something that must have occurred to someone else before I’d like to use the combined knowledge of stackoverflow to find out if there’s a standard / best-practice way of doing this that doesn’t clash with the php standard library, common frameworks etc.

  • 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-16T23:45:14+00:00Added an answer on May 16, 2026 at 11:45 pm

    I’m afraid there is no such thing. I’m not exactly sure what the reason is you need this, but consider the following options:

    Adding a toInt() method, casting in the class. You’re probably aware of this already.

    public function toInt()
    {
        return (int) $this->__toString();
    }
    

    Double casting outside the class, will result in an int.

    $int = (int) (string) $class;
    

    Make a special function outside the class:

    function intify($class)
    {
        return (int) (string) $class;
    }
    $int = intify($class);
    

    Of course the __toString() method can return a string with a number in it: return '123'. Usage outside the class might auto-cast this string to an integer.

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

Sidebar

Related Questions

Is there a way to tell the php complier that I want a specific
I was wondering is there a way in PHP that you could tell where
Is there any way to tell db4o to call class default constructor while activating
Is there a way to tell Visual Studio 2005 to not rearrange your SSIS
Is there say way to universally tell php to redirect to a certain page
Is there a way to tell PHP to run one last function, or somehow
Is there any way to tell Netbeans to highlight PHP code in HTML files?
Is there a way to tell subversion update/merge unless it would cause a conflict?
Is there a way to tell if a PHP variable contains a nowdoc string?
Is there any way to tell GCC to not initialize a particular global array

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.