Type casting for user defined objects

前端 未结 11 1557
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 01:35

Just like we do with __ToString, is there a way to define a method for casting?

$obj = (MyClass) $another_class_obj;
11条回答
  •  死守一世寂寞
    2020-11-29 01:49

    If casting for type hinting is all you're after, this works.

    if( is_object($dum_class_u_want) && $dum_class_u_want instanceof ClassYouWant )
    {
        // type hints working now
        $dum_class_u_want->is_smart_now();
    }
    

    Yep.

提交回复
热议问题