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

  • Home
  • SEARCH
  • 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 270207
In Process

The Archive Base Latest Questions

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

Is there a way to tell the php complier that I want a specific

  • 0

Is there a way to tell the php complier that I want a specific implicit conversion from one type to another?

A simple example:

class Integer
{
  public $val;
}

function ExampleFunc(Interger $i){...}

ExamFunc(333); // 333 -> Integer object with $val == 333.

[edit]… someone asked for an example. Here’s an example from c#. This is a boolean type that changes value after it has been accessed once.

    /// <summary>
    /// A Heisenberg style boolean that changes after it has been read. Defaults to false.
    /// </summary>
    public class hbool
    {
        private bool value;
        private bool changed = false;

        public hbool()
        {
            value = false;
        }

        public hbool(bool value)
        {
            this.value = value;
        }

        public static implicit operator bool(hbool item)
        {
            return item.Value;
        }

        public static implicit operator hbool(bool item)
        {
            return new hbool(item);
        }

        public bool Value
        {
            get
            {
                if (!changed)
                {
                    value = !value;
                    changed = true;
                    return !value;
                }
                return value;
            }
        }

        public void TouchValue()
        {
            bool value1 = Value;
        }

        public static hbool False
        {
            get { return new hbool(); }
        }

        public static hbool True
        {
            get { return new hbool(true); }
        }
    }

        [Test]
        public void hboolShouldChangeAfterRead()
        {
            hbool b = false;
            Assert.IsFalse(b);
            Assert.IsTrue(b);
            Assert.IsTrue(b);
            hbool b1 = false;
            Assert.IsFalse(b1);
            Assert.IsTrue(b1);
            Assert.IsTrue(b1);
            hbool b2 = true;
            Assert.IsTrue(b2);
            Assert.IsFalse(b2);
            Assert.IsFalse(b2);
            bool b3 = new hbool();
            Assert.IsFalse(b3);
            Assert.IsFalse(b3);
            Assert.IsFalse(b3);
        }
  • 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-11T23:57:45+00:00Added an answer on May 11, 2026 at 11:57 pm

    Long answer:

    I think it is very difficult (read impossible) for PHP to do an implicit conversion in this case.

    Remember: the fact that you call your class Integer is a hint to the human reader of the code, PHP does not understand that it actually is used to hold an integer. Also, the fact that it has an attribute called $val is a hint to a human reader that it should probably contain the value of your integer. Again PHP does not understand your code, it only executes it.

    At some point in your code you should do an explicit conversion. It might be possible that PHP has some nice syntactig sugar for that, but a first attempt would be something like:

    class Integer
    {
      public $val;
    
      function __construct($val) { $this->val = $val; }
    }
    
    function ExampleFunc($i){
        if (is_numeric($i)) { $iObj = new Integer($i); }
        ...
    }
    
    ExamFunc(333); // 333 -> Integer object with $val === 333.
    

    This is not as cool as you would like it, but again, it is possible that PHP has some syntactic sugar that will hide the explicit conversion more or less.

    Short version:

    In one way or another, you will need an explicit conversion

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

Sidebar

Related Questions

I was wondering is there a way in PHP that you could tell where
Is there a way to tell PHP to run one last function, or somehow
is there any way to tell Apache that it should restart the session and
Is there say way to universally tell php to redirect to a certain page
Is there any way to tell Netbeans to highlight PHP code in HTML files?
Is there a way to tell PHP how to convert your objects to ints?
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
Is there any way to tell db4o to call class default constructor while activating
Is there a way to tell excel to not display non-significant series in 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.