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.
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 new classname
statement.
$test = new myClass(123);
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 new myClass
is always the initialized object.
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:
<?php
class myClass {
var $input;
var $output;
function someOtherFunctionName($input) {
$output = 'You entered: ' . $input;
return $output;
}
}
$test = new myClass;
echo $test->someOtherFunctionName(123);
?>
If a constructor is indeed what you want to use, then note that since PHP 5, it is standard practice to use __construct()
instead of creating a method with the same name as the class:
<?php
class myClass {
var $input;
var $output;
function __construct($input) {
$this->input = $input; // This is a valid thing to do in a constructor
}
}
More on constructors and destructors in PHP 5 in the manual.