PHP 🔥

09 June, 2018

The following post is the notes I took for Laracasts "The PHP Practitioner" video course.

Language updates

(2004) PHP 5.0 object-oriented model ➡️
(2009) PHP 5.3 anonymous functions and namespaces in ➡️
(2012) PHP 5.4 traits

Variables

Use Double quotes for string interpolation. single quotes won't work.
👍 “hello $name" 👎 ‘hello $name'

alternatively you can concatenate. echo ‘Hello '.$name you could also add brackets around the variable “hello {$name}”

PHP with HTML

if the file contains just php, you can omit the php closing tag.

<?= is a shortcut for <?php echo otherwise end your code with ?>

Security

you can sanitize your input using the function: htmlspecialchars so it’ll treat users entering html as plain strings. Instead of: this is bold you'll see this is bold</b

Separation of concerns (separating responsibility)

Use the keyword 'require' to pull in everything from a file require = index.view.php

Classes

function dd($tasks) {
    foreach($tasks as $task) {
        var_dump($task);
    }
    die();
}

class Task {
    protected $description;
    protected $completed = false;

    public function __construct($description) {
        $this->description = $description;
    } 

    public function complete() {
        $this->completed = true;
    }

    public function isComplete()
    {
        return $this->completed;
    }
}
$tasks = [
    new Task('go to the store'),
    new Task('buy milk'),
    new Task('go to the library'),
];

dd($tasks); 

Database

PDO() stands for php data object. It offers an interface to connect to your database.

Handle exceptions

try/catch {}

Helpful hint

In some deprectated tutorials you might see something like mysql_connect(); avoid it.

Static methods

It's a way to make a methods globally accessible without requiring an instance. A static functions are not instance methods.

class Connection {
    public static function make() {

    }
}

// Instead of having to do this...
// $connection = new Connection();
// $connection->make();

//You can do this instead
Connection::make();

There's this concept of super global arrays

$GET, $POST, $SERVER
`var
dump(trim($SERVER['REQUESTURI']));`

Quick sidetrack, this is a good programming workflow to adopt.

  1. write code 2. see it fail 3. make it work

Run PHP with its built-in server

  1. php -S localhost:8080
  2. php file.php

arraymap, arrayfilter

while array filter takes an array and a closure function as parameters (in that order), for some reason its the other way round for array_map() function. These language inconsistencies are encapsulated with wrappers around those functions with collection classes.

When would you use array_map()?

When you need to modify something before returning it.

When to use array_map, array_filter and array_column?

  • array_filter() allows you to filter down a collection and then return a boolean to indicate if that item gets included in the result set.
  • arraymap() transforms an object in some way, maybe return a subset or converts it or wrap it in some other object. -arraycolumn() pulls a value from each item in the collection.

To be continued...