简体   繁体   中英

Access variable from another php file's class function

I have two php files. one for class(Library), this class name() function returns variable, I want to access returned variable name to another php file. thank u.

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;
?>

To get the name from your example class:

echo $data->name();

You can't use variable this . You only can use this when you refer to the current object. But you are referring to the one object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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