PHP | Classes
Like C++ and Java, PHP also supports object oriented programming
- Classes are the blueprints of objects. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: ‘object’.
- Class is a programmer-defined data type, which includes local methods and local variables.
- Class is a collection of objects. Object has properties and behavior.
Syntax: We define our own class by starting with the keyword ‘class’ followed by the name you want to give your new class.
<?php
class person {
}
?>
Note: We enclose a class using curly braces ( { } ) … just like you do with functions.
Given below are the programs to elaborate the use of class in Object Oriented Programming in PHP.
The programs will illustrate the examples given in the article.
Program 1:
<?php class GeeksforGeeks { // Constructor public function __construct(){ echo 'The class "' . __CLASS__ . '" was initiated!<br>'; } } // Create a new object $obj = new GeeksforGeeks; ?> |
Output:
The class "GeeksforGeeks" was initiated.
Program 2:
<?php class GeeksforGeeks { // Destructor public function __destruct(){ echo 'The class "' . __CLASS__ . '" was destroyed!'; } } // Create a new object $obj = new GeeksforGeeks; ?> |
Output:
The class "GeeksforGeeks" was destroyed.
Reference:
Classes in PHP
Recommended Posts:
- Classes in TypeScript
- Abstract Classes in PHP
- jQuery | Get and Set CSS Classes
- CSS | Pseudo-classes
- Wildcard Selectors (*, ^ and $) in CSS for classes
- When to use static vs instantiated classes in PHP?
- Use of :even and :odd pseudo-classes with list items in CSS
- jQuery | multiple classes Selector
- How to Add and Remove multiple classes in jQuery ?
- Routing in Node.js
- How to connect multiple MySQL databases on a single webpage ?
- How to append an element in two seconds using jQuery ?
- How to install XAMPP on Windows ?
- PHPUnit | assertNotContainsOnly() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



