2
\$\begingroup\$

Example code:

<?php
class MyClass {
 public $bar = array('a', 'b', 'c');
 public function getBar() {return $this->bar;}
 public function execute(array $fooArray) {
 foreach ($fooArray as $foo) {
 echo $foo.":".$this->checkBar($foo, $this->getBar())." ";// PASSED IN//
 //echo $foo.":".$this->checkBar($foo)." ";// RETRIEVED //
 }
 }
 // PASSED IN //
 public function checkBar($foo, array $fooCheck) {
 return in_array($foo, $fooCheck);
 }
 // RETRIEVED //
 /*public function checkBar($foo) {
 $fooCheck = $this->getBar();
 return in_array($foo, $fooCheck);
 }*/
}
$someClass = new MyClass();
$someClass->execute(array('a','f','c','g'));
?>

Are there any performance considerations to passing $fooCheck in as a variable into checkBar vs having MyClass::checkBar() handle calling the function itself?

asked Sep 1, 2011 at 15:36
\$\endgroup\$
1
  • \$\begingroup\$ Nope, it's all good. \$\endgroup\$ Commented Nov 12, 2011 at 3:47

1 Answer 1

2
\$\begingroup\$

Having (1)

public function execute(array $fooArray)
{
 foreach ($fooArray as $foo) {
 echo $foo . ":" . $this->checkBar($foo, $this->getBar()) . " ";
 }
}
public function checkBar($foo, array $fooCheck)
{
 return in_array($foo, $fooCheck);
}

is the exact same thing (performance wise) as (2)

public function execute(array $fooArray)
{
 foreach ($fooArray as $foo) {
 echo $foo . ":" . $this->checkBar($foo) . " ";
 }
}
public function checkBar($foo)
{
 return in_array($foo, $this->getBar());
}

In example (1), if you needed checkBar() to handle several different array inputs, then you would be able to differentiate them in execute(). Whereas example (2) would be better if you knew you would only be checking against a single array.

It's easier to expand code that's written like example (1).

answered Jun 15, 2014 at 4:21
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.