0

Hi I have a problem where I just can't figure out how to implement a construct function on this php that I have here:

class zendesk{

private $client;

function __construct() {

public function sync_organisations() {
    $loader = require LIBPATH . 'vendor/autoload.php';
    $loader->setPsr4("GuzzleHttp\\", APPPATH . '../vendor/guzzlehttp/guzzle/src/');

    $subdomain = "Name";
    $username  = "[email protected]"; // replace this with your registered email
    $token     = "token"; // replace this with your token

    $client = new ZendeskAPI($subdomain);
    $client->setAuth('basic', ['username' => $username, 'token' => $token]);}

Could somebody show me how to implement a public function __construct() {?

Thanks in advance for the help!

3
  • 1
    it's not clear what you are asking. The simple answer would be public function __construct(){} but I doubt this is (all) you are asking for. Maybe add how you want to use this class zendesk!? Commented Oct 16, 2018 at 8:23
  • @Jeff thank you for your comment. I will change the question! Commented Oct 16, 2018 at 8:29
  • An empty constructor does nothing so it does beg the question - which @Jeff also asks - why do you need a constructor, what work do you want to do inside it? Normally a constructor exists so you can pass values in or do initialisation work when you create your object. Commented Oct 16, 2018 at 8:35

1 Answer 1

1

PHP Constructors and Destructors

To create a Constructor in PHP use:

public function __construct() {
  //Code
}

So for example if you want to call your function sync_organisations() in the constructor, you could do following:

class zendesk{

    private $client;

    public function __construct() {
        $this->sync_organisations();
    }

    public function sync_organisations() {
        $loader = require LIBPATH . 'vendor/autoload.php';
        ...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

A small point - sync_organisations(); should be $this->sync_organisations();

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.