PHP HereDoc 引用系统常量、自定义常量、类和对象中常量与静态变量
定义常量
//define public const
define("TEST_DEFINDE_CONST_NAME","test define const var value");
类常量及静态变量
//define class const and static var
class TestClass
{
public const TEST_CLASS_CONST_NAME = "test class const var value";
public static $test_class_static_var = "test class static var value";
function __construct()
{
}
}
定义 HEREDOC
中引用函数(也可定义为全局函数和变量)
class TestPublic
{
public static function heredocGetConst($string): string
{
return $string;
}
//define const HGC
//public const HGC = __CLASS__ . '::' . 'heredocGetConst';
//define static function HGC
public static function HGC(){
static $HGC = null;
if(is_callable([__CLASS__ , "heredocGetConst"],callable_name:$HGC)){
return $HGC;
}
}
}
在 HEREDOC
中引用
class TestHeredocOutConst
{
public static function testHeredocOutConst(){
//$HGC = TestPublic::HGC;
$HGC = TestPublic::HGC();
$testClass = new TestClass();
$test_heredoc = <<<HEREDOC
test HereDoc output const :
define const(TEST_DEFINDE_CONST_NAME) : {$HGC(TEST_DEFINDE_CONST_NAME)}
class const(TestClass::TEST_CLASS_CONST_NAME) : {$HGC(TestClass::TEST_CLASS_CONST_NAME)}
class static var (TestClass::\$TEST_CLASS_CONST_NAME) : {$HGC(TestClass::$test_class_static_var)}
object const(TestClass::TEST_CLASS_CONST_NAME) : {$HGC($testClass::TEST_CLASS_CONST_NAME)}
object const(TestClass::\$test_class_static_var) : {$HGC($testClass::$test_class_static_var)}
system const(PHP_PREFIX) : {$HGC(PHP_PREFIX)};
ok!{$HGC(PHP_EOL)}
HEREDOC;
return $test_heredoc;
}
}
echo TestHeredocOutConst::testHeredocOutConst();
测试输出 output
test HereDoc output const : define const(TEST_DEFINDE_CONST_NAME) : test define const var value class const(TestClass::TEST_CLASS_CONST_NAME) : test class const var value class static var (TestClass::$TEST_CLASS_CONST_NAME) : test class static var value object const(TestClass::TEST_CLASS_CONST_NAME) : test class const var value object const(TestClass::$test_class_static_var) : test class static var value system const(PHP_PREFIX) : /usr/local/php8; ok!
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: