简体   繁体   English

从另一个PHP文件的类函数访问变量

[英]Access variable from another php file's class function

I have two php files. 我有两个php文件。 one for class(Library), this class name() function returns variable, I want to access returned variable name to another php file. 一个用于class(Library),这个class name()函数返回变量,我想将返回的变量名访问到另一个php文件。 thank u. 感谢你。

One.php One.php

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

Two.php Two.php

<?php
require_once("One.php");
$data = new One(); 
$data->name();

//$name = $this->name(); // I tried like this but not access
//echo $name;
?>

To get the name from your example class: 要从示例类中获取名称,请执行以下操作:

echo $data->name();

You can't use variable this . 您不能使用变量this You only can use this when you refer to the current object. 仅当引用当前对象时,才可以使用this But you are referring to the one object. 但是,您指的是one对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM