调试技巧·获取变量类型 ☛ get_debug_type()的使用

<?php
declare(strict_types=1);
ini_set('display_errors', 'On');
ini_set('error_reporting', E_ALL);

require_once __DIR__ . '/vendor/smarty-5.5.1/libs/Smarty.class.php';

$redis = new Redis();
$smarty = new Smarty\Smarty();

echo 'gettype(): ' . gettype($redis) . PHP_EOL; // gettype(): object
echo 'get_debug_type(): ' . get_debug_type($redis) . PHP_EOL; // get_debug_type(): Redis

echo 'gettype(): ' . gettype($smarty) . PHP_EOL; // gettype(): object
echo 'get_debug_type(): ' . get_debug_type($smarty) . PHP_EOL; // get_debug_type(): Smarty\Smarty


//========== 说明 ==========//
// 1、在调试时强烈建议使用get_debug_type(),而不是gettype(),尤其是获取实例的类名,
//    gettype()只能获取到笼统的object,而get_debug_type()可以获取具体类名。

Copyright © 2025 码农人生. All Rights Reserved