Classes (PHP)

Uit De Vliegende Brigade
(Doorverwezen vanaf OO (PHP))
Naar navigatie springen Naar zoeken springen
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

PHP is an object oriented programming language. This article is an introduction to classes and object oriented programming. It's not very PHP-specific.

Classes

In PHP, a "class" is a blueprint or template for creating objects. It defines a set of properties (variables) and methods (functions) that the objects created from the class will have. Classes are a fundamental concept in object-oriented programming (OOP), and they are used to model real-world entities and encapsulate data and behavior related to those entities:

  • Objects: Objects are instances of a class. You can create multiple objects from the same class, and each object will have its own set of properties and methods defined by the class
  • Properties: Properties (also known as member variables or attributes) are variables associated with a class. They store the data that represents the state of an object. Each object has its own copy of the class's properties
  • Methods: Methods are functions defined within a class. They encapsulate the behavior or actions that objects created from the class can perform. Methods can operate on the class's properties and interact with other objects
  • Encapsulation: Classes provide a way to encapsulate data and behavior. This means that the internal details of how a class works are hidden from the outside, and access to properties and methods is controlled through visibility modifiers (e.g., public, private, protected)
  • Inheritance: PHP supports inheritance, which allows you to create a new class that inherits properties and methods from an existing class. This promotes code reuse and allows you to create specialized classes based on more general ones
  • Polymorphism: Polymorphism is the ability to use different classes or objects in a consistent way. In PHP, this is often achieved through interfaces and abstract classes.

Example

###################################################################
# Class definition
###################################################################
#
class Person{
    #
    # Define properties   
    ########################################
    #
    public $name;
    public $age;


    # Define constructor method
    ########################################
    #
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }


    # Define method "introduce()"
    ########################################
    #
    public function introduce() {
        echo "Hi, I'm {$this->name} and I'm {$this->age} years old.";
    }
}


###################################################################
# Main script
###################################################################
#
#
# Create an object from the class
########################################
#
$person1 = new Person("Alice", 30);


# Call "introduce()" method
########################################
#
# Output: Hi, I'm Alice and I'm 30 years old.
#
$person1->introduce();

Get methods

Example get_class_methods [1]:

$sql="show databases;";
$result = $conn->query($sql);

print_r(get_class_methods($conn));

Get properties

Voorbeeld get_objects_vars [2]:

sql="show databases;";
$result = $conn->query($sql);

print_r(get_object_vars($conn));

See also

Sources