I have a class like this with a overloaded constructors
<?php
/*
Users Abstract Class
*/
abstract class User
{
protected $user_email;
protected $user_username;
protected $user_password;
protected $registred_date;
//Default constructor
function User()
{
}
//overloded constructor
function User($input_username,$input_email,$input_password)
{
__set($this->user_username,$input_username);
__set($this->user_email,$user_password);
__set($this->user_password,$input_password);
}
}
?>
Above code provides an error : error:Fatal error: Cannot redeclare User::User()
As other languages such as C++ and Java uses the above approach to overload the constructors how to do it in PHP OOP ?
Im using *PHP 5.3.2 in LAMP * which OOP concepts should fully supported in this version
PHP doesn't have overloading. It has a series of magic methods that are described as overloading in the manual (see: http://php.net/manual/en/language.oop5.overloading.php), but it's not what you're thinking of.
Also, as an aside, the correct way to write a constructor in PHP 5+ is to use the __construct method:
public function __construct(/* args */)
{
// constructor code
}
You can't overload methods based on their arguments at all. in your case the answer may be as simple as my answer to a similar question here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With