What does the PHP keyword var

Classes and Objects in PHP

Object-oriented programming is hard to imagine nowadays.
This chapter only deals with the basics of object orientation and only for PHP, since anything else would go beyond the scope.

Basics

Objects and classes are basically the same, only that classes describe the type and objects are the implementation, just as Integer is a data type and a variable of the Integer type. Class types are defined with the keyword.

class class // here definition of the class
{    
... class elements ...
}

$ Object = new class ();
// and here the implementation (created object)

In principle, an object is a collection of variables (called fields in the object orientation) with their own access functions, the so-called methods.

Constructor

A new instance of a class is created using the keyword and a so-called constructor is called. This is nothing more than a function that is used to initialize the variables, the so-called fields of the class. The constructor is declared as and must have the same name as the class.

class class
{
function class ($ name) {echo "$ name generated! n"; }
}

$ object = new class ("object");
// Output: 'Object created!'

Destructor

A destructor is a function of the object that is automatically called when it is destroyed. Unfortunately, as in C ++ or Object Pascal, these are not yet implemented in PHP.

this

The keyword is used within objects as a pointer to itself. So that PHP can recognize whether a function call or variable applies to the object itself or something outside of it, the keyword must be placed in front of it.

$ name = "name global";

class class
{

}
$ object = new class ("name object");
echo $ object-> name; // outputs $ name of the object
echo $ name; // print global $ name

Methods

Methods are the internal functions of an object and are actually used to access and manipulate the fields.

class circle
{
var $ radius = 2;



}

$ circle = new circle (4);
// creates new object with radius 4

echo $ circle-> area ();
// calculates the area of ​​the circle and outputs it

echo $ circle-> circumference ();
// calculates the circumference of the circle and outputs it

Properties and fields

In its object orientation, PHP does not really represent properties and fields; they are simulated by area-dependent variables.

class circle
{
var $ radius = 2;
function circle ($ radius)
  {


  }
}

You can declare the fields of an object in PHP in two different ways. The first is to set the variables using the keyword in the class block. The second is to declare the variable in the constructor, but don't forget the keyword in front of it. The only difference between the two ways is that when declaring in the object block (outside the constructor), no expressions can be assigned. This is only possible within the constructor.

Inheritance / Derivation

A parent class can "inherit" its methods and fields to a so-called derived class. This means that a derived class has its own methods and fields as well as those of the parent class. Derivations of classes are marked with the keyword.

class circle // parent class

var $ radius;


}

class cylinder extends circle // child class of circle

var $ height;
function cylinder ($ r, $ h)
  {


  }
function volume ()
  {

// Calling the method flaeche () from the parent class
  }
}

$ circle = new circle (4);
// Create a circle object with radius 4

echo $ circle-> area (), "n";
// outputs the area of ​​the circle

$ cylinder = new cylinder (2.5);
// Create cylinder with height 5 and radius 2

echo $ cylinder-> area (), "n";
// outputs the base area of ​​the cylinder

echo $ cylinder-> volume (), "n";
// outputs the volume of the cylinder

This example defines a class called Circle with a field and a method for calculating the area. Then another class with the name cylinder is defined, whereby this is derived from the class circle by the keyword and receives an additional field and a method. Despite the fact that we have not defined a method or field in the cylinder class, we can still access them and even get the correct result!

Access to parent classes

If a method is declared in a child class that has the name of a method of the parent class, this is "overwritten". In order to still be able to access the implementation of the inherited methods in this case, enter the name of the parent class followed by two colons and the method or field identifier.

class mother
{
var $ name = "mother";
function get_name ()

}

class child extends mother
{
var $ name = "child";
function get_name ()
  {
Mother :: get_name ();

  }
}

$ kind = new Kind ();
$ kind-> get_name ();
unset ($ child);

Here you can see that both the method of the class mother and that of the class child access the same field, but both are different methods.

Read more: ⯈ PHP and MySQL

about us