[英]How to access/or get variable in function?
<?php
class test
{
function __construct()
{
$methods = get_class_methods( get_class($this) )
foreach($methods as $method)
// question: how to get $get_me in function a() ?
echo $this->method():$get_me;
}
function a()
{
$get_me = "good, take me home.";
}
?>
我如何从外部函数a()访问$ get?
你不能。
如果你的函数a
执行是:
function a()
{
return "good, take me home.";
}
你可以做:
$get_me = $this->a();
在你的__construct
你不知道 您无法使用函数来从中获取任意变量。
如果变量必须在函数外部可用,则将该变量声明为类属性或从函数return
值。
一种方法是将$ get_me声明为属性
<?php
class test
{
public $get_me;
function __construct()
{
$methods = get_class_methods( get_class($this) )
foreach($methods as $method)
// question: how to get $get_me in function a() ?
$this->get_me;
}
function a()
{
$this->get_me = "good, take me home.";
}
?>
$result = functionname();
functionname()
{
..............//
return variablename;
}
因此,您可以在函数之外获取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.