0

Is there a way to use a object variable instantiated from a class in two functions?

Here's the code I've tried, but its just returning null:

class bookAppointmentsController extends APIController
{
    private $business;  

    public funcition check($key)
    {
        $this->business = new APIClass();
        $setconnection = $this->business->connectAPI($key);
    }

    public function book()
    {
        dd($this->business) //returns null
        $this->business->book();
    }
}

I am trying to use the $business object in two functions but it does not work, when I dd($business) it returns null

Any way to do this?

5
  • 2
    this is suppose to be normal. Best practices insist on controller being stateless, thus a call should not depend from a precedent call. Although you can make it, and your code does not work for this. With each request you re-bootstrap your application, meaning when calling the book action, the check action never occured before. You would have to store your object in a file / database / session or anything else that wouldn't be reset by a new call. Commented Nov 13, 2017 at 17:04
  • Any way to use this object in two functions? I am stuck . Commented Nov 13, 2017 at 17:05
  • This is a scooping issue this might help you : stackoverflow.com/a/19016830/5203821 Commented Nov 13, 2017 at 17:05
  • Are you calling the two functions during the same request? Otherwise, as @Unex commented, you need some way to persist $business between requests. Commented Nov 13, 2017 at 17:17
  • No, the requests are separate that's why I needed two functions. I just need to carry over the confirmation code from check to book but it's proving really hard to do Commented Nov 13, 2017 at 17:42

2 Answers 2

0

Move the instantiation to the constructor:

public function __construct(APIClass $business)
{
    $this->business = $business;
}

However, it would be better if you make Laravel do the heavy lifting and prepare the APIClass for you.

In your AppServicePorvider under the register method, you can create the APIClass

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
  $this->app->bind('APIClass', function ($app) {
        $api = new APIClass();
        // Do any logic required to prepare and check the api
        $key = config('API_KEY');
        $api->connectAPI($key);
        return $api;
    });

}

Check the documentations for more details.

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

1 Comment

Actually, it's not working. The second function does not carry the same object instance
0

Maybe the solution could be to make the variable Global

You could make the variable global:

function method( $args ) {
    global $newVar;
    $newVar = "Something";

}

function second_method() {
    global $newVar;
    echo $newVar;
}

Or you could return it from the first method and use it in the second method

public function check($key)
{
    $this->business = new APIClass();
    $setconnection = $this->business->connectAPI($key);
    return $this->business;
}

public function book()
{
   $business = check($key);
   $business->book();
}

Comments

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.