tetstvt
tetstvt

Reputation: 9

How to pass argument to constructor

I am trying to pass parameter to my class constructor but cant.

My method

public function any(){
    $config=config('myconfig.details');
    $service = MyService::class($config);
    $service = $service->details();

}

My class constructor

class MyService
{
    public function __construct($config)
    {
        $this->config = $config;
    }
}

Whats my Wong here I am getting error

Call to undefined method MyService::class()

Upvotes: 0

Views: 67

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

The correct syntax for creating an object with passing parameters to class constructor is:

$service = new MyService($config);

To create a new object, use the new statement to instantiate a class.

http://php.net/manual/en/language.types.object.php

Upvotes: 4

Related Questions