Jump to content

Call variable from another function in class


Go to solution Solved by gizmola,

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);
	}


}
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.

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.

Link to post
Share on other sites
  • Solution

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
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.