Object Access



Objects have two access states, static and non-static, both inside and outside the classroom . Non-static objects to the scope resolution operator -> to accessing the static object with the operator :: accessed by the operator. The examples we have made up to now were on non-static access. At the moment static access is used to access the objects. The static nature or object of access immediately after the appearance of the statement used static Provides statement with an instance of the class to be created without the need to be accessible directly from him the right class. The scope resolution operator for accessing these objects is ::operator is used. Static access is to provide direct access to the required use, or more precisely, a property or method to make it statically accessible, which usually contains information that does not change for the class. It is usually quite suitable for designing single-task methods. They are much faster in performance because they do not create an instance of the class for accessing static methods. Static access class definitions may need to be used frequently, especially for systems with very high memory consumption. It is best to make a decision on the basis of whether a method is static or not.

The object access statements are as follows;

$ this -> It is used to access the properties and methods of non-static classes from within the class.
self :: It is used to access the properties and methods of static classes from within the class.
parent :: It is used to access the properties and methods of the upper class from within the class.

 

 

# Non-Static Qualification and Method

To access non-static attributes and methods, the visibility is accessed by the -> operator from within or outside the class, depending on the width .

File: Car.php
class Car
{
    public $version = '5.2.7';

    public getVersion()
    {
        return '5.2.7';
    }
}
File: index.php
<?php require 'Car.php';

$car = new Car;

echo $car->version;
echo $car->getVersion();
5.2.7
5.2.7
Classroom Use

Within the class, non-static methods are accessed from within the class with $ this -> based on visibility width . $ this is a special predefined variable that represents the class in which it is located and provides access to the attributes and methods defined for that class. Can  be used in non- static methods. If you try to use $ this in a static method , you get an error.

File: Car.php
class Car
{
    public $version = '5.2.7';

    public getVersion()
    {
        return $this->version;
    }
}
File: index.php
<?php require 'Car.php';

$car = new Car;

$car->version = '5.3.0';

echo $car->getVersion();
5.3.0

 

 

# Static Qualification


Statically defined attributes are accessed with the :: operator. However, the class derived from new can not be accessed with the -> operator .

Syntax

The Static idiom must be used immediately before an attribute name. 

[ public | protected | private ] static $ exampleProperty

Sample 
File: Car.php
class Car
{
    public static $version = '5.2.7';
}

Let's show how to access the static defined property in our Car class above.

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

echo Car::$version;

Car::$version = '5.2.8';

echo Car::$version;
5.2.7
5.2.8

If you try to access a static property as follows, you will get an error at the warning level.

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

$car = new Car;

echo $car->version;
Notice : Accessing static property Car :: $ version as non static in  C: \ xampp \ htdocs \ test.php  on line 3
Notice : Undefined property: Car :: $ version in  C: \ xampp \ htdocs \ test.php  on line  3
Use in Classroom

A static defined property can not be accessed with $ this from within a class . Instead , access is provided via the self :: id. If you try to access $ this , you will get an error.

File: Car.php
class Car
{
    protected static $version = '5.2.7';

    public function getVersion()
    {
        return self::$version;
    }
}

 

 

# Static Method


Statically defined methods  are accessed with the :: operator. In contrast to the qualities, the class derived from new can also be accessed with the -> operator.

Syntax

The Static idiom must be used immediately before an attribute name. 

[ public | protected | private ] static function exampleFunction () {}

Sample 
File: Car.php
class Car
{
    public static $version = '5.2.7';

    public static getCreateDate()
    {
        return '20180404';
    }
}

Let us show you how to access the static defined method in our Car class above.

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

echo Car::getCreateDate();
20180404

A static method can also be accessed with a new sampling.

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

$car = new Car;

echo $car->getCreateDate();
20180404
Use in Classroom

A static defined method is accessed through the class self :: id. The $ this variant can not be used in a statically-defined method . Because the class instance is not created during static calls, $ this can not be created. Therefore, it can not be used in the static method.

File: Car.php
class Car
{
    private static $date = '20180404';

    public static getCreateDate()
    {
        return self::$date;
    }
}

If you try to use $ this in a static class , you get an error.

class Car
{
    private static $date = '20180404';

    public static getCreateDate()
    {
        return $this->date; # Hatalı kullanım!
    }
}
Notice : Accessing static property Car :: $ date as non static in  C: \ xampp \ htdocs \ test.php  on line  9