0

I know that this is a common question but the questions I've seen (about 10) confused even more than I've been.

My questions are included in the code as comments.

I have a class with three fields

public class Model
{
    public $prop1;
    public $prop2;
    public $prop3;

     public function _construct($params)  // doubt 1: Do I have to pass an array to a constructor, can't I pass the parameters individually
       {
                // doubt 2: How to assign the value to the instance variables
       }
}

$model = new \App\Model($whatToPuthere); //doubt 3: How to provide the constructor parameters
3
  • 1
    Have you read the PHP documentation about Classes and Objects? Everything you need to know for now is explained there, with examples. Commented Aug 28, 2017 at 15:19
  • 1
    There's a documentation for everything" If we follow this line of thinking there's no reason to have stackoverflow. If I stop to read the documentation for every problem I face it will take a long long time to complete my tasks. My question is very basic concept that one should have under his belt but in this moment I don't have the time to read the documentation. Commented Aug 28, 2017 at 15:30
  • Besides I read the documentation but I got confused by it too. Constructor in PHP is very confusing for me. You should have asked if I read the docs, before the downvote! Commented Aug 28, 2017 at 15:39

3 Answers 3

1

the proper way to do it is like :

public class Model
{
    public $prop1;
    public $prop2;
    public $prop3;

     public function __construct($prop1, $prop2, $prop3)  
     {
          $this->prop1 = $prop1; 
          $this->prop2 = $prop2; 
          $this->prop3 = $prop3; 
     }
}

$model = new \App\Model("prop1_value", "prop2_value", "prop3_value");
Sign up to request clarification or add additional context in comments.

7 Comments

Please add explanation too.
Your code is invalid. Your construct method is invalid.
Still not ready to upvote... $this->prop1 = $props1;... are you sure $props1 exists? :)
Look better. You are passing $prop1 but using $props1 while assigning.
@Spectarion you're right sir, i'm really tired i didn't see it, good catches (y)
|
0

You were pretty close, to access the current instance variables, you need to use $this. You also need to use 2 underscores for the construct method. Other than that, treat it as just another function.

class Model {

    public $prop1;
    public $prop2;
    public $prop3;

    public function __construct($prop1, $prop2, $prop3) {
        $this->prop1 = $prop1;
        $this->prop2 = $prop2;
        $this->prop3 = $prop2;
    }

}

$model = new \App\Model("prop1", "prop2", "prop3");

Comments

0

It is up to you to decide what is more convenient for your \App\Model class users. Here is an example working with fixed number of properties.

class Model
{
    public $prop1;
    public $prop2;
    public $prop3;

     public function __construct($prop1, $prop2, $prop3)
     {
       $this->prop1 = $prop1;
       $this->prop2 = $prop2;
       $this->prop3 = $prop3; 
     }
}

$model = new \App\Model('Property 1', 'Property 2', 'Property 3'); 

If you intend to work with dynamic number of properties, you should consider using array as parameters.

class Model
{
    public $prop1;
    public $prop2;
    public $prop3;

     public function __construct(array $properties)
     {
         $this->prop1 = $properties['prop1'];
         $this->prop2 = $properties['prop2'];
         $this->prop3 = $properties['prop3']; 
     }
}

$model = new \App\Model(
   array('prop1' => 'Property 1', 'prop2' => 'Property 2', 'prop3' => 'Property 3')
);

1 Comment

public classis not valid!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.