Classes (PHP): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
(5 tussenliggende versies door dezelfde gebruiker niet weergegeven) | |||
Regel 1: | Regel 1: | ||
− | == | + | 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 == | ||
<pre> | <pre> | ||
− | + | ################################################################### | |
+ | # 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 | ||
################################################################### | ################################################################### | ||
# | # | ||
− | |||
− | |||
# | # | ||
− | class | + | # 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(); |
− | |||
</pre> | </pre> | ||
== Get methods == | == Get methods == | ||
− | + | Example <code>get_class_methods</code> [https://www.php.net/manual/en/function.get-class-methods.php]: | |
<pre> | <pre> | ||
Regel 56: | Regel 87: | ||
</pre> | </pre> | ||
− | == | + | == See also == |
* [[Arrays (PHP)]] | * [[Arrays (PHP)]] | ||
+ | * [[Wp user list (WP-CLI) | wp user list (WP-CLI)]] | ||
− | == | + | == Sources == |
* https://www.php.net/manual/en/language.oop5.references.php | * https://www.php.net/manual/en/language.oop5.references.php |
Huidige versie van 3 nov 2023 om 10:59
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));