Snippets

Lauren Pittenger PHP #laurenlearnsstuff

Created by Lauren Pittenger last modified
<?php

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

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 

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

class Product
{
    // properties
    public $name;
    public $price;
    public $desc;
    
    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();

echo $p->getInfo(); 

?>
<?php

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
<?php

// Object Oriented PHP Basics

// Object Inheritence

class Product 
{

  public static $manufacturer = "Bart Taylor";

  public $name = 'default_name';
  public $price = 0;
  public $desc = 'default description';
  
  function __construct($name, $price, $desc) {
    $this->name = $name;
    $this->price = $price;
    $this->desc = $desc;
  }
  
  public function getInfo(){
    return "Product Name: ". $this->name;
  }
  
  public function getMaker(){
    return self::$manufacturer;
  }
}

class Soda extends Product
{
  public $flavor;
  
  function __construct($name, $price, $desc, $flavor) {
    // extends the parent class construct and also adds our own 
    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!

?>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.