Classes & Objects



Classes and Objects are the cause of OOP's existence. So if there is an Object Oriented Programming in the middle, then there are classes and objects. We mentioned that the code in the previous location needs to be clustered. This is exactly where the clustering process is done. Classes are what we use for clustering. The concept of object is a concept that includes the properties, methods and class constants in a class. In our narration on "Basic PHP Training", we have shown that variables, sequences and functions are defined with a certain syntax. In the same way, Classes need a certain syntax to be able to be created.

 

 

 

 

# Syntax


The simplest way to create a class is as follows.

class ExampleClassName
{
    # kodlar
}

Our class above does not currently have objects of its own.

Class Naming Conventions

There are some rules to consider when choosing a class name;

● The class name must begin with a letter or underscore (_).
● The class name can only contain letters, numbers, or underscores.
Key words reserved by PHP can not be used. Example: foreach

 

 

# Properties


Qualities are variables defined in classes. They have taken the qualification name because they are used in the classroom. Because variables are considered a property of that class within the class. You can think of this as a person's size, the presence of a class. 

Syntax

We said that qualities are like variables. However, they are defined according to certain rules when they are defined within the class.

public | protected | private [ static ] $ propertyName [= 'Property Value' ]; 

The public, protected, and static statements above will be discussed later.

Qualification Naming Rules

There are some rules to consider when choosing a qualification name;

● The attribute name must begin with a letter or underscore (_).
● The attribute name can only contain letters, numbers, or underscores.

Sample
class Human
{
    public $height;
    public $weight;
    public $age;

    # Kodlarınız
}

 

 

# Methods


Methods are functions defined within classes. Because they are used in the class, they have taken the method name. Because functions are considered to be a method of that class within a class. You can think of it as a way of life, a behavior of a class. Methods are sometimes used to access and modify the values ​​of attributes.

Syntax

We said that the methods are like functions. However, they are defined according to certain rules when they are defined within the class.

public | protected | private [ static ] function functionName ( $ p1 , $ p2 ... $ pN ) { # codes

Method Naming Rules

There are some rules to be considered when choosing a method name;

● The method must start with a letter or underscore (_).
● The method name can only contain letters, numbers, or underscores.

As an exception, method names can be selected from reserved keywords in PHP. Because that method belongs to a class, PHP will not conflict with its own function. 

Sample
class Human
{
    public $height;
    public $weight;
    public $age;

    public function lifeStyle()
    {
        return 'A stagnant life!';
    }
}

 

 

# Class Constants


Class constants are defined by the const expression. Class constants are accessed with the :: scope access operator .

Syntax

const CONSTANT_NAME = 'value' ;

Available Data Types 

Scalable values.( string | numeric | bool
Series.( array )

Use Constants
File: Human.php
<?php class Human
{
    const VERSION = '1.0.0';
    const ABOUT   = ['author' => 'ZN', 'date' => '20180404'];
}
File: index.php
<?php require 'Human.php';

echo Human::VERSION;
1.0.0

 

 

# How to Use?


The new statement is used to use the generated classes . The class usage statement is as follows.

Syntax

As a rule of a class, you can use it as follows.

$ instanceName = new ExampleClassName [([ $ p1 , $ p2 ... $ pN ])];

Create Class

Now as an example Humen Create a class.

File: Human.php
<?php 

class Human
{
    public $height;
    public $weight;
    public $age;

    public function lifeStyle()
    {
        return 'A stagnant life!';
    }
}
Include Class

What I now create Humen Let's add our class to the following file.

File: index.php
<?php require 'Human.php';

A require or include phrase is used to include another file in a file .

Use Class

Now let's use the class we have included according to the syntactic rule.

<?php require 'Human.php';

$human = new Human;

$ Human variant for the above usageHumenclass and can use all the publicly accessible properties and methods of the Human class .

Use Qualities

The public accessibility of a class is accessed through the -> operator of scope resolution operators .

<?php require 'Human.php';

$human = new Human;

$human->height = 180;
$human->weight = 75;
$human->age    = 30;

echo 'Height:' . $human->height . '<br>';
echo 'Weight:' . $human->weight . '<br>';
100
75
Use Methods

Public access methods of a class are accessed with the -> operator of the scope resolution operators .

<?php require 'Human.php';

$human = new Human;

echo $human->lifeStyle();
A stagnant life!