0

I'm wondering why I can't return result with getData() function? it is empty in browser (PHP 7.1), I'm trying to understand why.

I can print/echo it just fine o.O

class testObject
{
     public $data;

     function __construct($data) 
     {
         $this->data = $data;

     }

     function showInfo() 
     {
         print_r($this->data);
     }


     function getData() 
     {
         $str = $this->data;
         return $str;
     }
}
$data = 'test';
$a = (new testObject($data))->getData();
1
  • You're doing something wrong because this is working fine: ideone.com/5V3Fgz Commented Mar 14, 2018 at 19:17

2 Answers 2

1

Before $a You must write echo or print, because your method returns data, but you must tell what this data should be shown.

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

Comments

0

This works fine for me just running it in PHP Sandbox.

If you want to show it in the browser, put a echo or print after the $a.

Comments

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.