1

I'm creating a class that uses google.

I want to return the value as an object from another class.

Example:

$x = new SocialServices($key, $keySecret, $apiKey);

Here I call the Google() method which requires a google.php file and creates the new object

$x->Google();

Here I want return Google class as $this.

    public function Google() {
        require 'PHP/google.php';
        new Google($this->key, $this->secret, $this->apiKey, "#Google");
    }

When i call

$x->YouTube();

It will be works.

2
  • Tried to improve the wording, at least according to I interpreted the question Commented Apr 23, 2019 at 0:01
  • 1
    Thank you man... Commented Dec 25, 2021 at 5:40

1 Answer 1

2

I'm not sure I fully understood what you're asking, but I know for sure that you are missing a return in your method.

That means that the working method that returns a Google object would be

public function Google() {
    require 'PHP/google.php';
    return new Google($this->key, $this->secret, $this->apiKey, "#Google");
}

What I don't understand is what you mean by "Here I want return Google class as $this". I you want the method to return the same object to be able to concatenate, you can assign the Google object to an internal variable declared in the constructor and then just return $this right after the assignment.

By the way I would suggest you to avoid using the require, it's better if you use composer's autoloading.

Sign up to request clarification or add additional context in comments.

2 Comments

I tested but user must make $x = $x->Google(); but i want when user makes $x->Google(); $x = a Google class but not SocialServices();
@MDReal That's exactly what happens if you return the Google object as in my example, if you overwrite the value of $x with $x->Google() it will then be a Google object.

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.