访问另一个php文件中的变量';s类函数


Access variable from another php file's class function

我有两个php文件。一个用于class(Library),这个类名()函数返回变量,我想将返回的变量名访问到另一个php文件。谢谢你。

One.php

<?php 
class One
{
  public function name()
  {
   $name = "SampleName";
   return $name;
  }
}
?>

Two.php

<?php
require_once("One.php");
$data = new One(); 
$data->name();
//$name = $this->name(); // I tried like this but not access
//echo $name;
?>

从示例类中获取名称:

echo $data->name();

不能使用变量this。只有在引用当前对象时才能使用this。但是您指的是one对象。