Jump to content

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


}
Link to comment
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 comment
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
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.