Jump to content
jonnyenglish89

Call variable from another function in class

Recommended Posts

Hey guys,

How can I access the $currentAccount and $numberOfAccounts variables from the construct function and use them in the updateDB function without making them global variables?

<?php

class Client {


	public function __construct($start = [])
	{
		ini_set('display_errors', 1);
		ini_set('display_startup_errors', 1);
		error_reporting(E_ALL);
		$sql = mysqli_connect('localhost', 'root', '', 'test');
		$query = "SELECT * FROM users;";
		$ret = mysqli_fetch_all(mysqli_query($sql, $query) , MYSQLI_ASSOC);
		$this->print(count($ret) . ' Accounts Loaded!');
		$numberOfAccounts = (count($ret));
		foreach($ret as $k => $v) {
			$start = [];
			$start['name'] = $v['name'];
			$start['password'] = $v['password'];
			$currentAccount = 1;
			$this->print("processing account number {$currentAccount} of {$numberOfAccounts} ...");
			$currentAccount++;
			$this->start = $start;
			$this->handshake($start);
		}
	}

	public function handshake($start)
	{

		// makes a connection to an api with the $start array as parameters and returns $account_info

		$this->updateDB($account_info);
	}

	public function updateDB($account_info)
	{
		$this->print("processed account number {$currentAccount} of {$numberOfAccounts} ...");
	}

	public function print ($str)
	{
		print ('[' . date('H:i:s', time()) . '] ' . $str . PHP_EOL);
	}


}

Share this post


Link to post
Share on other sites

Easy: don't use a class for this. I see no reason for you to do so. All the code can go in one function just fine.

Share this post


Link to post
Share on other sites

Thanks, but the above code is not the code I am using – I wrote that as a simple example of my problem.

Taking away the class would mean changing a lot of code :(

Share this post


Link to post
Share on other sites

 

How can I access the $currentAccount and $numberOfAccounts variables from the construct function and use them in the updateDB function without making them global variables?

You can't.  

 

When you create an instance of a class, it's constructor function gets run.  Then and there.  Your [calling] code waits until that constructor completes before carrying on. 

 

By the time your code gets around to calling the updateDB function, the code execution and, more importantly, the local variables within the constructor are long gone, so there are no local variables for you to access! 

 

Now, the number of accounts could be a class-level variable, because it persists after the constructor has run, but any idea of the "current" account?  Probably not.

 

Regards,   Phill  W.

Share this post


Link to post
Share on other sites

If the code you posted does not accurately describe your situation then you need to post code that accurately describes your situation. The actual code itself, if at all possible.

Share this post


Link to post
Share on other sites

When you define your class, just have properties that store the values you need to ingest from the constructor. Typically you want these properties to be private or protected, so scope them accordingly.

 

 

 

class Client {
    private $currentAccount = 0;
    private $numberOfAccounts = 0;

    public function __construct($start = []) {
        //code
        $this->currentAccount = $currentAccount;
        //more code
        $this->numberOfAccounts = $numberOfAccounts;
    }
}
It looks like you would benefit from a quick read through the PHP manual section that covers classes and OOP

Share this post


Link to post
Share on other sites

thank you very much for the help guys! Unfortunately, I’m unable to share the actual code but you’ve helped me nonetheless

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.