PHP — The combination of Traits and Magic methods

Darshan
4 min readMar 19, 2023

Let’s say we’re building a program to manage a zoo, and we want to create a ZooAnimal class that all animal types in the zoo will inherit from. We want to use traits to add specific functionality to different animal types, and we want to use magic methods to make it easy to get and set animal properties.

we could use the ZooAnimal class with traits and magic methods to manage a zoo's animal database.

Suppose we have a database of all the animals in our zoo, and we want to be able to add new animals, update existing animal data, and retrieve animal data by ID. We can use the ZooAnimal class as a base class for all animal types, and use traits to add specific functionality to different animal types.

trait Swim {
public function swim() {
echo $this->name . " is swimming!\n";
}
}

class ZooAnimal {
private static $animals = [];

private $data = [];

public function __construct($name) {
$this->name = $name;
self::$animals[$this->getId()] = $this;
echo $this->name . " the " . get_class($this) . " has been added to the database!\n";
}

public function __get($name) {
return $this->data[$name];
}

public function __set($name, $value) {
$this->data[$name] = $value;
}

public function getId() {
return spl_object_id($this);
}

public static function getById($id) {
if (isset(self::$animals[$id])) {
return self::$animals[$id];
}
return null;
}
}

class Fish extends ZooAnimal {
use Swim;
}

class Bear extends ZooAnimal {
}

$fish1 = new Fish("Nemo");
$fish1->color = "orange";
$fish1->swim();

$fish2 = new Fish("Dory");
$fish2->color = "blue";
$fish2->swim();

$bear1 = new Bear("Yogi");
$bear1->color = "brown";

$bear2 = new Bear("Paddington");
$bear2->color = "black and white";

echo "The fish with ID " . $fish1->getId() . " is " . $fish1->color . " and its name is " . $fish1->name . "\n";
echo "The bear with ID " . $bear1->getId() . " is " . $bear1->color . " and its name is " . $bear1->name . "\n";

$fish1->color = "red";
echo "The fish with ID " . $fish1->getId() . " has been updated to " . $fish1->color . "\n";

$unknownAnimal = ZooAnimal::getById(123456);
if ($unknownAnimal === null) {
echo "Could not find animal with ID 123456.\n";
} else {
echo "The animal with ID 123456 is a " . get_class($unknownAnimal) . " named " . $unknownAnimal->name . "\n";
}

we define a Swim trait that contains a swim() method. We also define a ZooAnimal class that uses magic methods to get and set properties, and keeps track of all animal objects in a static array. The Fish class uses the Swim trait to add swimming functionality, while the Bear class does not.

When we create a new Fish object or Bear object, it is automatically added to the ZooAnimal::$animals array. We can then retrieve animal data by ID using the static ZooAnimal::getById() method.

we can create a versatile and reusable codebase that can be adapted to many different use cases.

This example demonstrates the power of object-oriented programming (OOP) concepts such as inheritance, traits, and magic methods, which can help us write more efficient and maintainable code. By using OOP principles, we can create code that is more organized, easier to understand, and more adaptable to changing requirements over time.

Picture this: You’re in a game of Mario Kart, and you’re trying to get ahead of the competition. In Java and C#, it’s like you’re limited to only using the basic banana peels and green shells. Sure, they’re helpful, but they can only do so much.

But in PHP, it’s like you’ve unlocked the secret weapon of traits! You can not only use the banana peels and green shells, but you can also use the red shells, blue shells, and even the elusive lightning bolt. Traits give you more options and flexibility to win the race, without being limited by the traditional class inheritance.

And then there’s Python and Ruby, who are like the cool kids who come up with their own unique ways of doing things. They have something called mixins, which are similar to traits, but they don’t always play well together. It’s like they’re all bringing different items to the potluck party, but no one knows who’s bringing what, and everyone ends up bringing cupcakes.

So in short, PHP traits are like the ultimate secret weapon in Mario Kart, giving you more options and flexibility than other programming languages. And let’s be real, who doesn’t want to win at Mario Kart?

Your support means a lot to us, and we look forward to hearing from you!

--

--

Darshan

Enthusiastic web developer, part time content writer