I am getting an undefined variable error on $instance, $username, $password when trying to use the class in snclient.php: ServiceNowClient. I tried making changing private to public as the function is puiblic but this gave an error too. It seems to me (adn from searching) that the private variables are not passing to the function? How do I solve this?

require_once("SNClient/SNClient.php");
$SNClient = new ServiceNowClient($instance, $username, $password);

snclient.php:

class ServiceNowClient
{
    /* Variables */
    private $instance = "xx";
    private $username = "xx";
    private $password = "xx";
    private $proxy = "";

    /* Constructor of the class
     * Required to get credentials to use authentification on Service-Now.
     * Example: $SNClient = new ServiceNowClient();
    */
    public function __construct($instance, $username, $password){
        

        
        $this->instance = $instance;
        $this->username = $username;
        $this->password = $password;

    }

Thanks!

    require_once("SNClient/SNClient.php");
    $SNClient = new ServiceNowClient($instance, $username, $password);
    

    Where do $instance, $username, and $password come from in this line (I'm guessing this is the line that the error is being reported on)?

    Incidentally, that "Example" line in the comment is incorrect; those parameters aren't optional.

      Thanks for the reply, yes the error is on this line:

      $SNClient = new ServiceNowClient($instance, $username, $password);

      They are defined in the snclient.php as :

      private $instance = "xx";
      private $username = "xx";
      private $password = "xx";

        Not those variables, the ones you use in the line
        $SNClient = new ServiceNowClient($instance, $username, $password);
        Where do those variables get their values from?

        You know, that encourages asking the question. If they "get their values" here:

            private $instance = "xx";
            private $username = "xx";
            private $password = "xx";
        

        Then why is changing them literally the first thing you do when creating the object?

                $this->instance = $instance;
                $this->username = $username;
                $this->password = $password;
        

        Like, what are you changing them to and what was the point of giving them values back when you declared them?

        Indeed, it looks like back when the comment on the constructor was written, you didn't have to supply anything:

            /* Constructor of the class
             * Required to get credentials to use authentification on Service-Now.
             * Example: $SNClient = new ServiceNowClient();
            */
        

        But since then the code has been changed:

            public function __construct($instance, $username, $password){
        

        and now you do have to supply those values. So you write:

        $SNClient = new ServiceNowClient($instance, $username, $password);
        

        and apparently you're now having trouble figuring out what $instance, $username, and $password here are supposed to be.

          That's not actually how that works. What you're seeing there is class property defaults. Do you see how the __construct method has $instance, $username, and $password parameters, then assigns that data to the class properties? What that means is that whenever you create a new instance of the ServiceNowClient class, you have to supply the $instance, $username, and $password values. So somewhere in your code you'll see new ServiceNowClient() - this is the problem. You need to have something more along the lines of this:

          $instance = new WhateverInstanceIs();
          $username = $_POST['get_the_username'];
          $password = password_hash($_POST['get_the_password']);
          
          $serviceNowClient = new ServiceNowClient($instance, $username, $password);

          Obviously you won't be able to copy and paste this code, but the point is that this class is being instantiated somewhere else in the code base, and it's not being instantiated correctly.

            Thanks to both. @maxxd if I set the credentials as you say, how will the private variables in SNClient.php get set?

            class ServiceNowClient
            {
            /* Variables */
            private $instance = "";
            private $username = "";
            private $password = "";
            private $proxy = "";

            /* Constructor of the class
             * Required to get credentials to use authentification on Service-Now.
             * Example: $SNClient = new ServiceNowClient();
            */
            public function __construct($instance, $username, $password){
                
            
                
                $this->instance = $instance;
                $this->username = $username;
                $this->password = $password;
             
            }

            The problem is not in the class itself (we think), but in the code that is trying to use it. You cannot do this...

            $SNClient = new ServiceNowClient($instance, $username, $password);
            

            ...until after you have actually defined $instance, $username, and $password in your main code, somewhere before the above command gets executed.

            If, in fact, you want to set those values within the class as default values, you could make each of them optional in the constructor, then not pass anything if you're happy with using them (though it may be easier to maintain if you are required to pass them in, as you can then set them in a separate config file, which also could then use some "secrets" mechanism to reduce their visibility to unauthorized people).

            class ServiceNowClient
            {
                /* Variables */
                private $instance = "xx";
                private $username = "xx";
                private $password = "xx";
                private $proxy = "";
            
            /* Constructor of the class
             * Required to get credentials to use authentification on Service-Now.
             * Example: $SNClient = new ServiceNowClient();
            */
            public function __construct($instance=null, $username=null, $password=null){
                if(!empty($instance) { $this->instance = $instance; }
                if(!empty($username) { $this->username = $username; }
                if(!empty($password) { $this->password = $password; }
            }
            

            Then if you simply do $foo = new ServiceNowClient();, it will be instantiated using the default values for those properties. If you want to use other values, then...

            $instance = 'whatever an instance is';
            $username = 'nogdog';
            $password = 'CorrectHorseBatteryStaple';
            $foo = new ServiceNowClient($instance, $username, $password);
            

              DesignerSeries if I set the credentials as you say, how will the private variables in SNClient.php get set?

              Your class's constructor is doing exactly that. It takes the values you pass to it from outside the class and assigns them to the private properties inside the class. Not trying to be insulting here, but may I suggest a little light reading.

                Ok I got it, @maxxd no insult - I get the properties, but I had a different expectation for this. I set the variables and got through the errors.

                Thanks

                  Write a Reply...