Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are using a method (functions in objects are called methods) that is the same name as the class. That is called the constructor, it has a special meaning in OOP.</p> <p>The constructor is never called separately, but is the method that gets called automatically when you initialize the object. Any parameters that method has, you append to the <code>new classname</code> statement.</p> <pre><code>$test = new myClass(123); </code></pre> <p>also, the constructor must never return a value. It is used only to do things while initializing the class, e.g. storing parameters. Any returned values will be lost, as the result of <code>new myClass</code> is <em>always</em> the initialized object.</p> <p>If you are just looking to create a method inside your class to return some text, then you need to change the method's name. This would work:</p> <pre><code>&lt;?php class myClass { var $input; var $output; function someOtherFunctionName($input) { $output = 'You entered: ' . $input; return $output; } } $test = new myClass; echo $test-&gt;someOtherFunctionName(123); ?&gt; </code></pre> <p>If a constructor is indeed what you want to use, then note that since PHP 5, it is standard practice to use <code>__construct()</code> instead of creating a method with the same name as the class:</p> <pre><code>&lt;?php class myClass { var $input; var $output; function __construct($input) { $this-&gt;input = $input; // This is a valid thing to do in a constructor } } </code></pre> <p>More on constructors and destructors in PHP 5 in the <a href="http://php.net/manual/en/language.oop5.decon.php" rel="nofollow noreferrer">manual</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload