2

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; 
        }

    }
    }
Thinker
  • 99
  • 1
  • 3
  • 13
  • Pls Check this http://stackoverflow.com/questions/2169448/why-cant-i-overload-constructors-in-php – Sethunath K M Apr 29 '12 at 10:26
  • possible duplicate of [Best way to do multiple constructors in PHP](http://stackoverflow.com/questions/1699796/best-way-to-do-multiple-constructors-in-php) – Gordon Apr 29 '12 at 10:30
  • This is just a horrible practice when it come to proper OOP – tereško Apr 29 '12 at 21:04

4 Answers4

2
  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.

  2. 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; 
        }
    }
    
Community
  • 1
  • 1
Xenon
  • 3,174
  • 18
  • 37
1

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.

Community
  • 1
  • 1
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38
0

For question 1 check out this post Best way to do multiple constructors in PHP

For question 2 you can filter out the comment_id attribute in your __set function.

public function __set($property_name,$value)
{
    if (property_exists($this, $property_name) && $property_name !== 'comment_id')
    {
        $this->$property_name = $value; 
    }

}
Community
  • 1
  • 1
ilanco
  • 9,581
  • 4
  • 32
  • 37
0

Question 1:

Is this the correct way to implement a constructor in PHP with 2 different set of parameters ?

Yes (well, at least a correct way, as other posters pointed out). One improvement I can think of would be to throw an exception in the default branch.

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?

Sure, you're almost there:

public function __set($property_name,$value) {
  if(property_exists($this,$property_name)) {
    if($property_name == "comment_id") {
      // throw an exception? ignore?
    } else {
      $this->$property_name = $value;
    }
  } else {
    throw new Exception(strtr("Class {class} does not have property {property}!", array(
      '{class}' => get_class($this),
      '{property}' => $property_name,
    ));
  }
}

But there's a limitation here, see this comment.

DCoder
  • 12,962
  • 4
  • 40
  • 62
  • I 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. – Gordon Apr 29 '12 at 10:35