PHP 类的隐式类型转换? [英] Implicit Type Conversion for PHP Classes?

查看:40
本文介绍了PHP 类的隐式类型转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉 php 编译器我想要从一种类型到另一种类型的特定隐式转换?

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

一个简单的例子:

class Integer
{
  public $val;
}

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

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

[edit]... 有人要求举个例子.这是一个来自 c# 的例子.这是一个布尔类型,它在访问一次后更改值.

[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);
        }

推荐答案

长答案:

我认为在这种情况下,PHP 很难(阅读不可能)进行隐式转换.

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

请记住:您将类称为 Integer 是对代码的人类读者的一个提示,PHP 不明白它实际上是用来保存整数的.此外,它有一个名为 $val 的属性这一事实暗示人类读者它可能应该包含整数的值.PHP 再次无法理解您的代码,它只会执行它.

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.

在您的代码中的某个时刻,您应该进行显式转换.PHP 可能有一些很好的语法糖,但第一次尝试是这样的:

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.

这并不像您希望的那么酷,但同样,PHP 可能有一些语法糖,可以或多或少地隐藏显式转换.

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.

简短版本:

无论如何,您都需要一个显式转换

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

这篇关于PHP 类的隐式类型转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆