Posted by: phillipnb | June 24, 2010

PHP – Warning: Missing argument 1 for……


I am not a PHP guru but when I thought about the effort that I put in to help my friend to solve this error, it struck to me that it will be great if I could share it with others. This error can be solved in a few seconds provided you are alert and think in the direction that PHP is complaining but sometimes when such an error is thrown when you run an application of not less than 10,000 lines of PHP code, even the best coder gets confused for a few minutes. The error that we are going to discuss here is:

Warning: Missing argument 1 for createaccount::__construct(), called in C:\blog\php\createaccount.php on line 12 and defined in C:\blog\php\createaccount.php on line 5
Notice: Undefined variable: bcd in C:\blog\php\createaccount.php on line 7
bcd = Tool completed successfully

The code that we are talking about is:

class createaccount
{
 public function __construct($bcd)
 {  echo "\n bcd = $bcd";
 }
}
$testVar = "createaccount";
$myObj = new $testVar();

The first thing that I did was to clean up the code a bit by following proper naming conventions. The code that was improved in terms of its looks is shown below:

class CreateAccount
{
public function __construct($bankCode)
{
echo "\n bank code = $bankCode";
}
} $aNewClass = "CreateAccount";
$myObj = new $aNewClass();
echo "\n\n Done..";

This did not and does not solve the actual propblem with this code: I have a added a new line to write “Done..” to show that the error thrown is not fatal and that the code execution continues to the end.

We all know that a class in PHP is a set of php statements that perform a specific task. We also know that the constructor of a class is automatically called by the keyword ‘new’ while creating an instance of the class and the constructors can accept arguments. With this knowledge, let us take a second look at the error: Looking at the error we can see that PHP is complaining that the class is not passing the variable for the argument 1 of its constructor. If you closely look at the code we can see that when an instance of the class is created, we forgot to pass the variable which the class constructor is expecting. Let us add that variable:

class CreateAccount
{
public function __construct($bankCode)
{
echo "\n bank code = $bankCode";
}
}
$bCode = '100'; $aNewClass = "CreateAccount";
$myObj = new $aNewClass($bCode);
echo "\n Done";

If we run this new code, we can see that PHP is not complaining anymore about the missing argument. It is also good if the variable in the constructor also has a default value so that if for some reason the value of the variable is not passed to the constructor, then the default value will kick in without the code throwing a warning or error. So the completed code with all the formalities will be:
class CreateAccount
{
public function __construct($bankCode = '999')
{
echo "\n bank code = $bankCode";
}
} $bCode = '100';
$aNewClass = "CreateAccount";
$myObj = new $aNewClass($bCode);
echo "\n Done";

Enjoy

Advertisements

Responses

  1. interesting!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: