Examples

In this example, we first define a base class and an extension of the class. The base class describes a general vegetable, whether it is edible, and what is its color. The subclass Spinach adds a method to cook it and another to find out if it is cooked.

Example #1 Class Definitions

Vegetable

<?php

class Vegetable {
    public 
$edible;

    public 
$color;

    public function 
__construct($edible$color "green")
    {
        
$this->edible $edible;
        
$this->color $color;
    }

    public function 
isEdible()
    {
        return 
$this->edible;
    }

    public function 
getColor()
    {
        return 
$this->color;
    }
}

?>

Spinach

<?php

class Spinach extends Vegetable {
    public 
$cooked false;

    public function 
__construct()
    {
        
parent::__construct(true"green");
    }

    public function 
cook()
    {
        
$this->cooked true;
    }

    public function 
isCooked()
    {
        return 
$this->cooked;
    }
}

?>

We then instantiate 2 objects from these classes and print out information about them, including their class parentage. We also define some utility functions, mainly to have a nice printout of the variables.

Example #2 test_script.php

<?php

// register autoloader to load classes
spl_autoload_register();

function 
printProperties($obj)
{
    foreach (
get_object_vars($obj) as $prop => $val) {
        echo 
"\t$prop = $val\n";
    }
}

function 
printMethods($obj)
{
    
$arr get_class_methods(get_class($obj));
    foreach (
$arr as $method) {
        echo 
"\tfunction $method()\n";
    }
}

function 
objectBelongsTo($obj$class)
{
    if (
is_subclass_of($obj$class)) {
        echo 
"Object belongs to class " get_class($obj);
        echo 
", a subclass of $class\n";
    } else {
        echo 
"Object does not belong to a subclass of $class\n";
    }
}

// instantiate 2 objects
$veggie = new Vegetable(true"blue");
$leafy = new Spinach();

// print out information about objects
echo "veggie: CLASS " get_class($veggie) . "\n";
echo 
"leafy: CLASS " get_class($leafy);
echo 
", PARENT " get_parent_class($leafy) . "\n";

// show veggie properties
echo "\nveggie: Properties\n";
printProperties($veggie);

// and leafy methods
echo "\nleafy: Methods\n";
printMethods($leafy);

echo 
"\nParentage:\n";
objectBelongsTo($leafySpinach::class);
objectBelongsTo($leafyVegetable::class);

?>

The above examples will output:

veggie: CLASS Vegetable
leafy: CLASS Spinach, PARENT Vegetable

veggie: Properties
        edible = 1
        color = blue

leafy: Methods
        function __construct()
        function cook()
        function isCooked()
        function isEdible()
        function getColor()

Parentage:
Object does not belong to a subclass of Spinach
Object belongs to class Spinach, a subclass of Vegetable

One important thing to note in the example above is that the object $leafy is an instance of the class Spinach which is a subclass of Vegetable.

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial