tags:

views:

240

answers:

5

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);
     }
A: 

No, PHP is not a strongly typed language.

Malfist
+3  A: 

PHP5 has type hinting, with limitations: http://ca2.php.net/manual/en/language.oop5.typehinting.php

Specified types must be objects or array, so built in types such as string and int are not allowed.

This is not a conversion, but will throw an error if an object of the specified type is not passed to the method or function, as in your example.

GApple
+1  A: 

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

dwergkees
I guess I'm asking for what that "syntactic sugar" is. I've used and like it in c# and was looking for something similar in PHP. Might be out of luck here.
Will Shaver
Care to show the same example in C# code? I'm interested and maybe it gives a hint in how to achieve the same effect php5
dwergkees
I figured your question was Java-like... the Java Integer class has a special link to the primitive type, that as far as I know can't be replicated by user code (though I'm far from a Java expert). The problem is PHP doesn't have any sort of link like that in the first place. I think the best you can do is wrap your type checking and conversion into another function so it's reusable, and your ExampleFunc() can just call toIntegerObj($i) as it first line; not caring what it was passed, and knowing it now has an Integer object.
GApple
I've added a c# example.
Will Shaver
A: 

Are you asking how to type cast? You can do:

$ php -r 'var_dump("333");'
string(3) "333"
$ php -r 'var_dump((int)"333");'
int(333)

Otherwise PHP is weakly typed, so in general you don't need to do it. It's implied by the language. Eg. if a function takes a numeric argument, then that number can be string, integer or float all the same; The value is converted if needed. That's why you can for example echo 33 without an error.

troelskn
A: 

PHP is not strongly typed.

You want something like, whenever you pass an hbool instance into a function that expects a bool it would convert automatically using your implicit conversor.

What happens is that no function "expects" a bool, it's all dynamically typed. You can force an explicit conversion by calling (int)$something but if that's the case you can then pass $something->to_otherthing() instead for non-automatic conversions.

PS: I'm sure im not being clear here. I will try to edit this answer after I eat something :)

Carlos Lima