Jump to content


Photo

Call variable from another function in class


Best Answer gizmola, 01 May 2018 - 07:39 PM

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 Go to the full post


  • Please log in to reply
6 replies to this topic

#1 jonnyenglish89

jonnyenglish89
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 01 May 2018 - 09:00 AM

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


}


#2 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,874 posts
  • LocationWA

Posted 01 May 2018 - 09:12 AM

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.
"Basically, I think the general rule of thumb is: if someone really wants the blood that's inside of your body, and they're like a vampire, or a dracula, or some sort of man-squito, then that's probably okay. A dracula and a man-squito are made for removing things like blood and swords from inside your body. That's basically fine. If something wants to get at your blood and they're, say, some kind of murdersaurus, or maybe a really big frog, that's where the problems start to arise. A really big frog is not made for removing blood, and your blood knows this, which is why it is so vehement about wanting to stay in your body instead of coming out. Unfortunately this will not deter a really big frog because a really big frog is full of things like prizes, and value, and quite a lot of hatred, and it would really rather like to replace any and all of those things with your blood, and basically by any means possible." --slumbermancer

#3 jonnyenglish89

jonnyenglish89
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 01 May 2018 - 09:43 AM

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 :(



#4 Phi11W

Phi11W
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 01 May 2018 - 10:32 AM

 

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.



#5 requinix

requinix
  • Administrators
  • Impoverished Administrator
  • 9,874 posts
  • LocationWA

Posted 01 May 2018 - 02:30 PM

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.
"Basically, I think the general rule of thumb is: if someone really wants the blood that's inside of your body, and they're like a vampire, or a dracula, or some sort of man-squito, then that's probably okay. A dracula and a man-squito are made for removing things like blood and swords from inside your body. That's basically fine. If something wants to get at your blood and they're, say, some kind of murdersaurus, or maybe a really big frog, that's where the problems start to arise. A really big frog is not made for removing blood, and your blood knows this, which is why it is so vehement about wanting to stay in your body instead of coming out. Unfortunately this will not deter a really big frog because a really big frog is full of things like prizes, and value, and quite a lot of hatred, and it would really rather like to replace any and all of those things with your blood, and basically by any means possible." --slumbermancer

#6 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,722 posts
  • LocationLos Angeles, CA USA

Posted 01 May 2018 - 07:39 PM   Best Answer

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

#7 jonnyenglish89

jonnyenglish89
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 02 May 2018 - 11:44 AM

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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users