jonnyenglish89 Posted May 1, 2018 Share Posted May 1, 2018 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); } } Quote Link to comment Share on other sites More sharing options...
requinix Posted May 1, 2018 Share Posted May 1, 2018 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. Quote Link to comment Share on other sites More sharing options...
jonnyenglish89 Posted May 1, 2018 Author Share Posted May 1, 2018 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 Quote Link to comment Share on other sites More sharing options...
Phi11W Posted May 1, 2018 Share Posted May 1, 2018 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 1, 2018 Share Posted May 1, 2018 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. Quote Link to comment Share on other sites More sharing options...
Solution gizmola Posted May 1, 2018 Solution Share Posted May 1, 2018 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 Quote Link to comment Share on other sites More sharing options...
jonnyenglish89 Posted May 2, 2018 Author Share Posted May 2, 2018 thank you very much for the help guys! Unfortunately, I’m unable to share the actual code ☹ but you’ve helped me nonetheless Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.