Last active
August 29, 2015 14:14
-
-
Save jazzsequence/c1eaeb342129dfd4ce11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class My_Class { | |
// some variable I want to use later | |
protected $var_a = ''; | |
// another variable I want to use later | |
protected $var_b = ''; | |
public function __construct() { | |
// assign values to my global variables | |
$this->var_a = array( 'apples', 'oranges', 'bananas' ); | |
$this->var_b = array( 'pizza', 'beer', 'chips' ); | |
} | |
/** | |
* function that will loop through all of our foods | |
* | |
* @param bool $healthy true for healthy foods, false for unhealthy foods | |
*/ | |
public function my_loop( $healthy = true ) { | |
if ( $healthy ) { | |
$food = $this->var_a; // if we're showing healthy food | |
} else { | |
$food = $this->var_b; | |
} | |
foreach( $food as $nom ) { | |
$noms[] = $nom; | |
} | |
return $noms; | |
} | |
// this function will take the output of my_loop and echo it | |
public function echo_my_loop() { | |
$foods = $this->my_loop( false ); | |
echo sprintf( 'I eat %s, %s and %s all day.', $foods[0], $foods[1], $foods[2] ); | |
// will output "I eat pizza, beer and chips all day." | |
} | |
} | |
$My_Class = new My_Class; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment