Thủ Thuật về Hướng dẫn php constructor multiple parameters Mới Nhất
Bùi Bình Minh đang tìm kiếm từ khóa Hướng dẫn php constructor multiple parameters được Update vào lúc : 2022-09-27 23:36:23 . Với phương châm chia sẻ Kinh Nghiệm Hướng dẫn trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi tham khảo Post vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.Updated Answer:
echo ''; // option 1 - combination of both tadeck's and my previous answer class foo function __construct() $arg_names = array('firstname', 'lastname', 'age'); $arg_list = func_get_args(); for ($i = 0; $i < func_num_args(); $i++) $this->$arg_names[$i] = $arg_list[$i]; $foo = new foo('John', 'Doe', 25); print_r($foo); // option 2 - by default, PHP lets you set arbitrary properties in objects, even // if their classes don't have that property defined - negating the need for __set() // you will need to set properties one by one however, rather than passing them as // parameters class bar $bar = new bar(); $bar->firstname="John"; $bar->lastname="Doe"; $bar->age = 25; print_r($bar);Result:
Nội dung chính
- Updated Answer:Previous Answer:
Previous Answer:
'; for ($i = 0; $i < func_num_args(); $i++) echo 'Argument '.$i.' is: '.$arg_list[$i].'', "n"; $a = new Person('John'); $b = new Person('Jane', 'Doe'); $c = new Person('John', 'Doe', '25'); ?>
Result:
Argument 0 is: John Argument 0 is: Jane Argument 1 is: Doe Argument 0 is: John Argument 1 is: Doe Argument 2 is: 25Constructors are special thành viên functions for initial settings of newly created object instances from a class.
In PHP, a constructor is a method named __construct(), which is called by the keyword new after creating the object. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor arguments for the parameters.
Constructors are the very basic building blocks that define the future object and its nature. You can say that the Constructors are the blueprints for object creation providing values for thành viên functions and thành viên variables.
Constructor types:
- Default Constructor: It has no parameters, but the values to the default constructor can be passed dynamically.Parameterized Constructor: It takes the parameters, and also you can pass different values to the data members.Copy
Constructor: It accepts the address of the other objects as a parameter.
Note: PHP lacks support for declaring multiple constructors of different numbers of parameters for a class unlike languages such as Java.
Multiple Constructors: More than one constructor in a single class for initializing instances are available.
Example 1: In the following example, we create a script and try to declare multiple constructors. We will assign the name of a student using one constructor and age with another constructor.
PHP
class Student
public function __construct(string $name)
public function __construct(string $name, string $age)
$obj1 = new Student('Akshit');
$obj2 = new Student('Akshit', '12');
?>
Output:
PHP Fatal error: Cannot redeclare Student::__construct() in /home/33a7c36527d199adf721ab261035d4f7.php on line 10We have to use different methods to use Multiple Constructors in PHP. Some are listed below.
Approach:
- Get all the assigned arguments by using PHP func_get_arg() to get a mentioned value from the argument passed as the
parameters.We will first count the number of arguments passed to the constructor by using PHP func_num_args().By knowing type of arguments using PHP gettype() or data-type specific function
like is_int().Name your method as per the number of arguments and check its existence using PHP method_exists().Call the suitable function using call_user_func_array() by passing the original arguments as call_user_func_array()’s second argument.
Example 2:
PHP
class Student
public function ConstructorWithArgument1($arg1)
echo('Constructor with 1 parameter called: '.$arg1)."
";
public function ConstructorWithArgument2($arg1, $arg2)
echo('Constructor with 2 parameters called:
'.$arg1.','.$arg2)."
";
public function ConstructorWithArgument3($arg1, $arg2, $arg3)
echo('Constructor with 3 parameters called:
'.$arg1.','.$arg2.','.$arg3)."
";
public function __construct()
$arguments = func_get_args();
$numberOfArguments = func_num_args();
if (method_exists($this, $function =
'ConstructorWithArgument'.$numberOfArguments))
call_user_func_array(
array($this, $function), $arguments);
$obj = new Student('Akshit');
$obj = new Student('Akshit','Nikita');
$obj = new Student('Akshit','Nikita','Ritesh');
?>
Output
Constructor with 1 parameter called: Akshit Constructor with 2 parameters called: Akshit,Nikita Constructor with 3 parameters called: Akshit,Nikita,RiteshApproach:
- Check the type of argument that is passed with the constructor. It can be done using
gettype()or data-type specific function like is_int().Determine the type and initialize the instance variable.
Example 3: The following example demonstrates multiple constructors with different data types like string or integer.
PHP
class Student
public $id;
public $name;
public function __construct($idOrName)
if(is_int($idOrName))
$this->id=$idOrName;
echo "ID is initialized using constructor"."
";
else
$this->name = $idOrName;
echo
"Name is initialized using constructor"."
";
public function setID($id)
$this->id = $id;
public function setName($name)
$this->name = $name;
public function getinfo()
echo "ID : ". $this->id."
";
echo
"Name : ". $this->name."
";
$student = new Student("Akshit");
$student->setID(1);
$student->getinfo();
$student2 = new Student(2);
$student2->setName("Nikita");
$student2->getinfo();
?>
Output
Name is initialized using constructor ID : 1 Name : Akshit ID is initialized using constructor ID : 2 Name : NikitaTải thêm tài liệu liên quan đến nội dung bài viết Hướng dẫn php constructor multiple parameters programming php PHP controller constructor PHP class parameters PHP empty constructor