09 June, 2018
The following post is the notes I took for Laracasts "The PHP Practitioner" video course.
(2004) PHP 5.0 object-oriented model ➡️
(2009) PHP 5.3 anonymous functions and namespaces in ➡️
(2012) PHP 5.4 traits
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}”
if the file contains just php, you can omit the php closing tag.
<?=
is a shortcut for <?php echo
otherwise end your code with ?>
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 <b>this is bold</b>
Use the keyword 'require' to pull in everything from a file
require = index.view.php
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);
PDO() stands for php data object. It offers an interface to connect to your database.
try/catch {}
In some deprectated tutorials you might see something like mysql_connect();
avoid it.
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['REQUEST_URI']));
php -S localhost:8080
php file.php
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 you need to modify something before returning it.
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.
array_map() transforms an object in some way, maybe return a subset or converts it or wrap it in some other object.
-array_column() pulls a value from each item in the collection.
To be continued...