Skip to content

Instantly share code, notes, and snippets.

@lepittenger
Last active August 29, 2015 14:25
Show Gist options
  • Save lepittenger/975c3ad548b793df2b02 to your computer and use it in GitHub Desktop.
Save lepittenger/975c3ad548b793df2b02 to your computer and use it in GitHub Desktop.
Lauren Learns PHP
<?php
// Object Oriented PHP Basics
try {
// this is where the code we want to test goes
} catch (Exception $e) {
// Inspect $e
$e->getMessage(); // gets message that was passed into the exception constructor when the exception was thrown
$e->getCode(); // returns only the integer of the error code that was passed
$e->getFile(); // gets the file in which the error was actually generated
$e->getTrace(); // returns multidimensional array containing the trace methods that lead up to the exception (method, class, file, argument data)
$e->getTraceAsString(); // same as getTrace except in string format
$e->__toString();
}
<?php
// PHP Functions
function hello() {
return 'Hello, World';
}
// $greeting is equal to what our hello function returns
$greeting = hello();
echo $greeting;
/* *****
TWO
***** */
function hello($name) {
if($name == 'Lauren'){
return 'Hello, Lauren';
} else {
return 'Hello, stranger';
}
}
// $greeting is equal to what our hello function returns
$greeting = hello('Chris');
echo $greeting;
/* *****
THREE
***** */
function add_up($a, $b) {
$arr = array(
$a,
$b,
$a + $b
);
return $arr;
}
$value = add_up(2, 4);
// now we get back our sum value, but we also have the values that were passed in, in case we should need them.
echo $value[2]
/* *****
PHP VARIABLE FUNCTIONS
***** */
function answer(){
return 42;
}
function add_up($a, $b) {
return $a + $b;
}
$func = 'add_up';
$num = $func(5, 10);
/* *****
CLOSURES
anonymous functions that can access variables outside of the function scope
***** */
$name = 'Lauren';
$greet = function() use($name) {
echo "Hello, $name.";
};
$greet();
/* *****
BUILT-IN FUNCTIONS
***** */
// strlen -- return an integer of string length
$phrase = "We only hit what we aim for";
$len = strlen($phrase);
// echo $len;
// substr -- sub string -- returns a string starting at a position
substr($phrase, 0);
// strpos -- string position - find the first instance of a substring's position inside of a string
$start = strpos($phrase, 'hit');
echo substr($phrase, $start);
/* *****
ARRAY FUNCTIONS
***** */
$names = array(
'Mike' => 'Frog',
'Chris' => 'Teacher',
'Hampton' => 'Teacher'
);
var_dump(array_keys($names));
function print_info($value, $key){
echo "$key is a $value.";
}
array_walk($names, 'print_info');
<?php
// Object Oriented PHP Basics
class Product
{
public static $manufacturer = "Bart Taylor";
public $name = 'default_name';
public $price = 0;
public $desc = 'default description';
// constructor method
function __construct($name, $price, $desc){
$this->name = $name;
$this->price = $price;
$this->desc = $desc;
}
//methods
public function getInfo(){
return "Product Name: ". $this->name;
}
public function getMaker(){
return self::$manufacturer;
}
}
// Soda class inherits Product class's properties and methods
class Soda extends Product
{
public $flavor;
function __construct($name, $price, $desc){
// extends the parent class construct and also adds our own property
parent::__construct($name, $price, $desc);
$this->flavor = $flavor;
}
public function getInfo(){
return "Product Name: ". $this->name . " Flavor: ". $this->flavor;
}
}
$shirt = new Product("Space Juice T-Shirt", 20, "Awesome Grey T-shirt");
$soda = new Soda("Space Juice Soda", 2, "Thirst Mutilator", "Grape");
echo $soda->getInfo();
echo $shirt::$manufacturer;
// Bart Taylor
echo $shirt->getMaker();
// Bart Taylor
echo $shirt->manufacturer;
// ERROR undefined property!
?>
<?php
// Object Oriented PHP Basics
interface Rideable {
public function howToRide();
}
class Skateboard implements Rideable {
public function howToRide(){
$steps = array();
$steps[] = "Balance your front foot on the board.";
$steps[] = "Kick, then push with the other foot.";
$steps[] = "Don't fall down!";
return $steps;
}
}
?>
<?php
// Object Oriented PHP Basics
class Product
{
// properties
public $name = 'default_name';
public $price = 0;
public $desc = 'default description';
// constructor method
function __construct($name, $price, $desc){
$this->name = $name;
$this->price = $price;
$this->desc = $desc;
}
//methods
public function getInfo(){
return "Product Name: ". $this->name;
}
}
$p = new Product();
$shirt = new Product("Space Juice T-Shirt", 20, "Awesome Grey T-shirt");
$soda = new Product("Space Juice Soda", 2, "Grape Flavored T-Shirt");
echo $shirt->getInfo(); // returns 'Product Name: default_name'
?>
<?php
// Object Oriented PHP Basics
method_exists( object, method_name ); // returns true or false
/* ******** */
// EXAMPLES
/* ******** */
return method_exists("Product", "getPrice");
// will return true as long as we have defined a getPrice method in our Product class, otherwise we can expect False
$p = new Product("Name", 20, "Description");
return method_exists($p, "getPrice");
/* ************************* */
is_subclass_of(object, class_name);
class Product
{
// our parent class
}
class Soda extends Product
{
// child of product
}
$s = new Soda();
is_subclass_of($s, "Product");
// this example would return true because $s, our new Soda instance is a subclass of Product
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment