1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You, it's right. and I am sorry to ask one more question, how can access returned array.
Glad i could help. If i answered your question please mark my answer as correct for other users with the same question/problem. If "name()" returns an array you can do this: $name = $data->name(); echo $name['phone'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.