MasterACE14 Posted January 9, 2010 Share Posted January 9, 2010 Hello, I'm sure I've missed the obvious on this one. Say I have something like the following(nonsense example)... $arr['random'] = "Hello" class hey { public function say_hi() { echo $arr['random']; } } Obviously that doesn't work. But how would I get to use that array from 'outside' of the class, 'inside' of it without actually putting the array inside the class? Thanks, Ace Quote Link to comment https://forums.phpfreaks.com/topic/187798-pass-an-array-to-a-class/ Share on other sites More sharing options...
mikesta707 Posted January 9, 2010 Share Posted January 9, 2010 pass it as an argument into a method? Quote Link to comment https://forums.phpfreaks.com/topic/187798-pass-an-array-to-a-class/#findComment-991566 Share on other sites More sharing options...
trq Posted January 9, 2010 Share Posted January 9, 2010 You can pass it to the individual method itself or to the constructor if you need it within more than one method. class hey { public function say_hi($a) { echo $arr['random']; } } $arr['random'] = "Hello"; $h = new hey; $h->say_hi($arr); $arr['random'] = "Hello" class hey { private $a; public function __construct(array $a) { $this->a = $a; } public function say_hi() { echo $this->a['random']; } } $arr['random'] = "Hello"; $h = new hey($arr); $h->say_hi(); Quote Link to comment https://forums.phpfreaks.com/topic/187798-pass-an-array-to-a-class/#findComment-991567 Share on other sites More sharing options...
suryass Posted January 9, 2010 Share Posted January 9, 2010 using an object to call that variable Quote Link to comment https://forums.phpfreaks.com/topic/187798-pass-an-array-to-a-class/#findComment-991568 Share on other sites More sharing options...
MasterACE14 Posted January 9, 2010 Author Share Posted January 9, 2010 ah finally got it! Cheers thorpe the 'construct' way of doing it was what I was after! Quote Link to comment https://forums.phpfreaks.com/topic/187798-pass-an-array-to-a-class/#findComment-991573 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.