Defining Class In PHP

There are many techniques in creating the PHP projects for your need and one of them is by defining the class in PHP and extending that class as required. By using this technique, your PHP project will get reduced too much and could be completed in lesser time than required. Hence, here we will describe about how to define class in PHP.

In order to create a web development in an advanced way, as well as completing in less time and less coding for the developing PHP project, one should use the technique developed in the PHP programming language, which is the use of class. The code provided below shows you an example of defining the class in PHP and about how to use them in your PHP project. By using this technique of class, which is used in PHP, you can make your project easy to be setup, which is not possible only by using the basic PHP concept. Here, is the small information on defining class in PHP. Basic class definitions begin with the keyword class, followed by a class name and a pair of curly braces which enclose the definitions of the properties and methods belonging to the class. For eg:

<!DOCTYPE html>
<meta charset="UTF-8">
<title>Defining Class In PHP</title>
class MySimpleClass { // defining the class in php
public $val = "I'm inside class"; // property declaration in php class
public function displayval() { // method declaration in php class
echo "Hello World!<br/>";
echo "I'm inside the class MySimpleClass.<br/>";
$var = new MySimpleClass();

echo "<hr>";
if (class_exists("MySimpleClass")) { // checking that the class is defined or not
echo "MySimpleClass class has been defined. <br />";
} else {
echo "MySimpleClass class is not defined! <br />";

echo "<hr>";
if (method_exists('MySimpleClass', 'displayval')) { // checking that the method exists or not
echo "displayval method exists.<br />";
} else {
echo "displayval method does not exist.<br />";

The above example shows the way to define the class in PHP. The respective comments above shows their respective properties in order to make you understand about what the above code does. The result of the above program is shown below:


Here, we see the result as first, it displays the result of the class name MySimpleClass, as it is being called first time. The second result is the result of the class which existed, ie, MySimpleClass result exists as we have checked it. And finally, the third result is that we have added function in the class MySimpleClass, so the function here matches the query provided hence returns the true result.

Now, we can assume that you are familiar about how to define class in PHP and can do more with it, in your PHP project from the above simple example. Also, the main reason to use the class in PHP is that, it can be extended as required via the child class, ie, inheritance can take place in it which can save much of your time while you are creating your PHP project.

Post navigation

Bishal Napit

Bishal Napit is a WordPress theme developer from Tansen, Palpa, with a passion to learn more on WordPress.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.