PHP OOP classes recipe.php cookbook.php
// recipe.php <?php class Recipe { private $title; public $ingredients = array(); public $instruction = array(); public $yield; public $tag = array(); public $source = 'Alena Holligan'; private $measurements = array( "tsp", "tbsp", "cup", "oz", "lb", "fl oz", "pint", "quart", "gallon" ); public function displayRecipe() { return $this->title . "by" . $this->source; } public function addIngredient($item, $amount = null, $measure=null) { if ($amount != null && !is_float($amount) && !is_int($amount)) { exit("The amount must be a float: ") . gettype($amount) . " $amount given"); } if ($measure != null && !in_array(strtolower($measure), $this->measurements)) { exit("Please enter a valid measurement: " . implode(", ", $this->measurements)); } $this->ingredients[] = array ( "item" => ucwords($item), "amount" => $amount, "measure" => strtolower($measure) ); } public function getIngredients() { return $this->ingredients; } public function setTitle($title) { $this->title = ucword($title); } public function getTitle() { return $this->title; } } ?> // cookbook.php <?php include "classes/recipes.php"; $recipe1 = new Recipe(); echo $recipe1->source; $recipe1->source("Grandma Holligan"); echo $recipe1->source; $recipe1->setTitle("My first recipe"); $recipe1->getTitle(); $recipe1->addIngredient("egg",1); $recipe1->addIngredient("flour",2,"cup"); $recipe2 = new Recipe(); $recipe2->source = "Betty Crocker"; $recipe1->setTitle = "My second recipe"; echo $recipe1->source; echo $recipe2->source; echo $recipe1->displayRecipe(); echo $recipe2->displayRecipe(); foreach ($recipe1->getIngredients() as $ing) { echo "\n" . $ing["amount"] . " " . $ing["measure"] . " " . $ing["item"]; } var_dump($recipe1); ?>
<?php class Render { public static function displayDimensions($size) { return $size[0] . " x " . $size[1]; } public static function detailsKitchen($room) { return "Kitchen Dimensions: " . self::displayDimensions($room->getDimensions()); } } ?>
Some magic constants:
CLASS FILE
<?php class Example { public function __construct($title = null) { $this->setTitle($title); } public function __toString() { $output = "You are calling a " . __CLASS__ . " object with the title \""; $output .= $this->getTitle() . "\""; $output .= "\nIt is stored in " . basename(__FILE__) . " at " . __DIR__ . "."; $output .= "\nThis display is from line " . __LINE__ . " in method " . __METHOD__; $output .= "\nThe following methods are available for objects of this class: \n"; $output .= implode("\n", get_class_methods(__CLASS__)); return $output; } } class Render { public function __toString() { $output = "The following methods are available for " . __CLASS__ . " objects: \n"; $output .= implode("\n", get_class_methods(__CLASS__)); return $output; } } $example = new Example("Name"); echo $example; ?> <?php class Fish { public $common_name; public $flavor; public $record_weight; public function __construct($name, $flavor, $record) { $this->common_name = $name; $this->flavor = $flavor; $this->record_weight = $record; } public function getInfo() { return "A {$this->common_name} is an {$this->flavor} flavored fish. The world record weight is {$this->record_weight}."; } } $bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces"); ?>
recipecollection.php
<? class RecipeCollection { private $title; private $recipes = array(); // has constructor here and setter and getters public function addRecipe($recipe) { $this->recipes[] = $recipe; } public function getRecipe() { return $this->recipes; } } ?>
Create $myArray = array();
Push into $myArray[] = "Something";
Push to associative $myArray['key'] = "Value";
Create numeric $myArray = array('value', 'value2');
Create associative $a = array('key'=>'val');
Print from numeric echo $myArray[0];
Print from associative echo $myArray['key'];
Associative arrays Keys are strings
Numeric arrays Keys are numbers: 0,1,2,3,4
array_diff (arr1, arr2 ...) array_filter (arr, function) array_flip (arr) array_intersect (arr1, arr2 ...) array_merge (arr1, arr2 ...) array_pop (arr) array_push (arr, var1, var2 ...) array_reverse (arr) array_keys(array $array [, mixed $search_value = null [, bool $strict = false ]] ) array_search (needle, arr) array_walk (arr, function) count (count) in_array (needle, haystack)
// ARRAY EXAMPLES
<?php // add code below this comment class Subdivision { public $houses = array(); public function filterHouseColor($color) { $return = array(); foreach ($this->houses as $house) { if ($house->roof_color == $color || $house->wall_color == $color) { $return[] = $house; } } return $return; } } ?> <? php public function getCombinedIngredients() { $ingredients = array(); foreach ($this->recipes as $recipe) { foreach($recipe->getIngredients() as $ing) { $item = $ing["item"]; if (strpos($item, ",")) { $item = strstr($item, ",", true); } if (in_array($item."s", $ingredients)) { $item.="s"; } else if (in_array(substr($item, 0, -1), $ingredients)) { $item = substr($item, 0, -1); } $ingredients[$item] = array ( $ing["amount"], $ing["measure"] ); } } return $ingredients; } ?>
if (condition) { ... } elseif (condition) { ... } else { ... }
FOR loop for (initialize; condition; update) { ... }
WHILE loop while (condition) { ... }
FOREACH loop foreach ($array as $value) { ... }
DO WHILE do { ... ;} while (condition)
SWITCH ($s) { case 1: ... break; case 2: ... break; default: ... }
<?php $bool = false; ?> <?php if ($bool) : ?> <div> <p><?php echo "Bool is true"?></p> </div> <?php elseif (!$bool) : ?> <div> <p><?php echo "Elseif works"?></p> </div> <?php else : ?> <div> <p><?php echo "Bool is false"?></p> </div> <?php endif; ?>
isset() test for variable exists empty() test for empty variable mail($to, $subject, $msg, 'From: ' . $email) mail function mysqlifetch_array($result) fetch each row of a query (in $result) header() send a header from the server is_numeric() test to see if a value is number exit() causes script to stop immediately trim($string) trims leading and trailing spaces mysqli_realescape_string($string) escapes special characters str_replace('a', 'b', $string) replace a with b in a string explode(', ' , $string) make string into array implode(', " $string) make array into string substr ($string, start, len) grabs a substring preg_match('regex', $string) matches regular expressions preg_replace('regex', $replace, $string) replaces characters in a string by regex
ereg (pattern, str) split (pattern, str) ereg_replace (pattern, replace, str) preg_grep (pattern, arr) preg_match (pattern, str) preg_match_all (pattern, str, arr) preg_replace (pattern, replace, str) preg_split (pattern, str)
crypt (str, salt) explode (sep, str) implode (glue, arr) nl2br (str) sprintf (frmt, args) strip_tags (str, allowed_tags) str_replace (search, replace, str) strpos (str, needle) strrev (str) strstr (str, needle) strtolower (str) strtoupper (str) substr (string, start, len)
clearstatcache () copy (source, dest) fclose (handle) fgets (handle, len) file (file) filemtime (file) filesize (file) file_exists (file) fopen (file, mode) fread (handle, len) fwrite (handle, str) readfile (file)clearstatcache () copy (source, dest) fclose (handle) fgets (handle, len) file (file) filemtime (file) filesize (file) file_exists (file) fopen (file, mode) fread (handle, len) fwrite (handle, str) readfile (file)
checkdate (month, day, year) date (format, timestamp) getdate (timestamp) mktime (hr, min, sec, month, day, yr) strftime (formatstring, timestamp) strtotime (str) time ()
Y 4 digit year (2008) y 2 digit year (08) F Long month (January) M Short month (Jan) m Month ⁴ (01 to 12) n Month (1 to 12) D Short day name (Mon) l Long day name (Monday) (lowercase L) d Day ⁴ (01 to 31) j Day (1 to 31)
h 12 Hour ⁴ (01 to 12) g 12 Hour (1 to 12) H 24 Hour ⁴ (00 to 23) G 24 Hour (0 to 23) i Minutes ⁴ (00 to 59) s Seconds ⁴ (00 to 59)
w Day of week ¹ (0 to 6) z Day of year (0 to 365) W Week of year ² (1 to 53) t Days in month (28 to 31)
a am or pm A AM or PM B Swatch Internet Time (000 to 999) S Ordinal Suffix (st, nd, rd, th)
T Timezone of machine (GMT) Z Timezone offset (seconds) O GMT offset (hours) (+0200) I Daylight saving (1 or 0) L Leap year (1 or 0)
U Seconds since Epoch ³ c ISO 8601 (PHP 5) (2008-07-31T18:30:13+01:00) r RFC 2822 (Thu, 31 Jul 2008 18:30:13 +0100)
$locations = Timber::get_terms('locations'); $data = []; $exclusion = []; foreach($locations as $location) { $data[] = [ "location" => $location, "posts" => Locations::getPostsForLocation($location,$count,$exclusion) ]; // add posts with current ids to array to check against foreach ($data as $key => $value) { if ($value["posts"]) { $posts = $value["posts"]; foreach($posts as $key => $value) { if (in_array($value->id, $exclusion)) { unset($posts[$key]); } else { array_push($exclusion, $value->id); } } } } }