tags:

views:

66

answers:

5

hi

<?php
class myClass {
 var $input;
 var $output;

 function myClass($input) {
  $output = 'You entered: ' . $input;
  return $output;
 }
}

$test = new myClass;
echo $test->myClass(123);

?>

this works, but returns this warning:

Warning: Missing argument 1 for myClass::myClass()

I read in to this, and seems that the constructor is expecting a value, so by adding:

function myClass($input='')

the warning is removed, but this seems so unnecessary?

could someone enlighten me as to why it's required to define a value to prevent that warning?

thanks for any pointers

A: 

php thinks that your "myClass" function is a constructor (because its name matches the class name) and calls it automatically after "new". If you don't want this, just rename the function:

class MyClass {
   function foo($input) { ... }
...
}

$obj = new MyClass;
$obj->foo(bar);
stereofrog
+1  A: 

Because in these class myClass is the constructor, so it needs to get a variable in construction time, like this

$test = new myClass(123);

you could try setting the function like a normal method instead

class myClass {
 var $input;
 var $output;
 function myClass(){}
 function setInput($input) {
  $output = 'You entered: ' . $input;
  return $output;
 }
}

$test = new myClass;
echo $test->setInput(123);
markcial
A: 

If you want some parameters of methods/functions be optional you can specify their default values in the declaration like you did function myClass($input=NULL).

However you are learning PHP4 and it's no longer supported by the developers, have a look at PHP5.

Raveren
+1  A: 

The function you defined, myClass, is your constructor. That is the old php4 syntax which is still valid in php5.

So when you generate an object, the function is run automatically and as you are not sending a value when you instantiate the object, you get a warning.

You should either use:

$test = new myClass(123);

or name the function in your class differently if you don´t want it to be the constructor.

jeroen
+3  A: 

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.

Pekka
perfect explanation ~ the material i'm reading is a good few years old so i guess its outdated. thanks.
Ross