oop PHP more than one constructors
Solution 1
There are many ways of declaring 'multiple' constructors in PHP, but none of them are the 'correct' way of doing so (since PHP technically doesn't allow it). I don't see anything wrong with how you're doing it there, however. If you'd like more information, check out this Stack Overflow question.
-
You could just use an
if
statement. Something like this, perhaps?public function __set($property_name,$value) { $hidden_properties = array( 'comment_id', 'any_other_properties' ); if(!in_array($property_name, $hidden_properties) && property_exists($this, $property_name)) { $this->$property_name = $value; } }
Solution 2
As has already been shown here and Best way to do multiple constructors in PHP, there are many ways of declaring multiple
constructors in PHP.
Here's another example:
<?php
class myClass {
public function __construct() {
$get_arguments = func_get_args();
$number_of_arguments = func_num_args();
if (method_exists($this, $method_name = '__construct'.$number_of_arguments)) {
call_user_func_array(array($this, $method_name), $get_arguments);
}
}
public function __construct1($argument1) {
echo 'constructor with 1 parameter ' . $argument1 . "\n";
}
public function __construct2($argument1, $argument2) {
echo 'constructor with 2 parameter ' . $argument1 . ' ' . $argument2 . "\n";
}
public function __construct3($argument1, $argument2, $argument3) {
echo 'constructor with 3 parameter ' . $argument1 . ' ' . $argument2 . ' ' . $argument3 . "\n";
}
}
$object1 = new myClass('BUET');
$object2 = new myClass('BUET', 'is');
$object3 = new myClass('BUET', 'is', 'Best.');
Source: the easiest way to use and understand multiple constructors:
Hope this helps.
Comments
-
Thinker almost 2 years
I am learning alone PHP from websites. I have some OOP background on other languages.
Question 1:
Is this the correct way to implement a constructor in PHP with 2 different set of parameters ?
Question 2:
When I'm coming to __set magic method in PHP I want that the comment_id in this class cannot be changed from __set function. Can this be possibly done in PHP?class Comment{ private $comment_id; private $image_id; private $author_id; private $comment_text; private $created_at; public function __construct() { $arguments = func_get_args(); $num = sizeof($arguments) switch($num) { case 0; break; case 3: this->image_id = $arguments[0]; this->author_id = $arguments[1]; this->comment_text = $argument[2]; break; case 5: this->comment_id = $arguments[0]; this->image_id = $arguments[1]; this->author_id = $argument[2]; this->comment_text = $argument[3]; this->created_at = $argument[4]; break; default: break; } } public function __get($property) { if(property_exists($this,$property)) { return $this->$property; } } public function __set($property_name,$value) { if(property_exists($this,$property_name)) { $this->$property_name = $value; } } }
-
Gordon about 12 yearsI would argue the answer to Q1 is No since ctors are for setting an object into a valid state and by allowing zero arguments to be passed to the ctor none of the sets are required for that purpose hence they should be either in setters, static factory methods or a dedicated factory. Likewise, while the OP could add all that clutter into
__set
s/he is better off making all the mutatable fields public and just provide a getter or magic__get
for comment_id. -
Wanjia over 5 yearsWay to hard to find this, but finally found it.