Skip to content

Commit

Permalink
Fix is_object() function for objects inside mixed (#1207)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkornaukhov03 authored Jan 14, 2025
1 parent e997390 commit e10967d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime-common/core/core-types/definition/mixed.inl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void mixed::init_from(T &&v) {
if (unlikely(!ptr_to_obj)) {
php_error("Internal error. Trying to set invalid object to mixed");
}
type_ = type::OBJECT;
type_ = v.is_null() ? type::NUL : type::OBJECT;
} else {
auto type_and_value_ref = get_type_and_value_ptr(v);
type_ = type_and_value_ref.first;
Expand Down
4 changes: 4 additions & 0 deletions runtime-common/core/runtime-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,10 @@ bool f$is_object(const class_instance<T> &v) {
return !v.is_null();
}

template<>
inline bool f$is_object(const mixed &v) {
return v.is_object();
}

template<class T>
bool f$is_integer(const T &v) {
Expand Down
43 changes: 43 additions & 0 deletions tests/phpt/mixed/non_primitive/017_is_object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@ok
<?php
require_once 'kphp_tester_include.php';

class A {};

/** @return A */
function getObj(int $x) {
if ($x == 0) {
return null;
}
return new A();
}

/** @param $x mixed */
function is_obj_to_str($x) {
if (is_object($x)) {
return "true";
}
return "false";
}

function test() {
/** @var mixed */
$x;
$a = new A();

$x = to_mixed($a);
echo is_obj_to_str($x) . "\n";

$x = 123;
echo is_obj_to_str($x) . "\n";

#ifndef KPHP
echo "false\n"; // PHP emits fatal error
return;
#endif
$a = getObj(0);
$x = to_mixed($a);
echo is_obj_to_str($x) . "\n";
}

test();

0 comments on commit e10967d

Please sign in to comment.