PHP Advanced Introduction
PHP Advanced is a course for programs that need deeper control over classes, choices and data. Each lesson adds one advanced idea and proves it with small programs.
A class with a trait prints a line
The Logger trait holds one log method. The Page class takes the trait, and the page object runs the method.
<?php
trait Logger {
public function log($message) {
echo "Log: " . $message . "\n";
}
}
class Page {
use Logger;
}
$page = new Page();
$page->log("Page opened");An interface-typed function prints a line
The Greeter interface names one greet method. The welcome function names Greeter as its parameter type, so the Host object fits the call.
<?php
interface Greeter {
public function greet();
}
class Host implements Greeter {
public function greet() {
echo "Hello and welcome\n";
}
}
function welcome(Greeter $greeter) {
$greeter->greet();
}
welcome(new Host());Example
<?php
class Product {
public $name = "";
public function show() {
echo "Product: " . $this->name . "\n";
}
}
$stock = ["Pen" => 4, "Notebook" => 12];
foreach ($stock as $name => $count) {
$product = new Product();
$product->name = $name;
$product->show();
if ($count >= 10) {
echo "Stock: plenty\n";
} else {
echo "Stock: low\n";
}
}Key points
- Match choices and fixed value sets come first in the course.
- Class lessons add locked values, placed data and close inspection of classes.
- Call lessons pass arguments in new ways and turn methods into values.
- Later lessons pause work and resume it in small steps.
- Type lessons show how values change form and how a strict mode can stop that change.
- Error lessons sort errors into a family and catch each kind in its own way.
- Data lessons use ready-made stacks, queues, heaps and walkers.
- The last lessons look inside PHP at shared values and at how PHP runs faster on a server.
In this example, the stock array pairs each product name with a count. The foreach loop takes one pair at a time and makes a Product object for the name. The if check prints plenty for a count of 10 or more and low for a smaller count.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does this course do with the skills from the earlier courses?