php object oriented method

php object oriented static method usage What ...
php object oriented static method usage

What is a static method?

Not all variables and methods are called by creating objects. It can be called directly by adding static keywords to variables and methods.

The syntax format for calling a static member is:

Key:: static member

The keywords can be:

(1) self, used when static members are called inside a class.

(2) The class name of the static member, which is used when calling the static member inside the class outside the class.

Note: in static methods, only static variables can be called, not ordinary variables; while ordinary methods can call static variables.

<?php class Math{ //Static method for square calculation static function squared($input){ return $input*$input; } } echo Math::squared(3);

The operation result is: 9

The above is a simple instance. It is worth noting that the $this keyword cannot be used in static methods, because there may be no object instances to reference. Because static methods do not need to instantiate objects.

Using static members, in addition to not having to instantiate the object, another function is to save the modified static data after the object is destroyed, so as to continue to use it next time. This concept is more abstract. Let's take an example to analyze it.

<?php class Play{ static $num = 0; function showNum(){ echo 'num is :'.self::$num; self::$num++; } } $play1 = new Play(); $play1->showNum(); echo '<br>'; $play2 = new Play(); $play2->showNum(); echo '<br>'; echo 'num is :'.Play::$num;

matters needing attention:

Static methods are very easy to use. They do not need to instantiate objects. When the class is first loaded, static member space has been given. But nothing can be abused. Because once the static member declaration is too much and the space is occupied all the time, it will affect the running speed and function of the system, so remember: although things are good, you can't be greedy!

php object oriented: member methods, member variables, constants of classes

Define a person's class and create a running member method

<?php class Person{ //Define member variables public $name; public $age; public $gender; public function run(){ echo 'People in the tower'; } } $person = new Person(); $person->name = 'Demacia'; $person->run();

Class, then there will be constants. Constant means the quantity that will not change. It is a constant value.

To define constants, we use const.

<?php class Person{ public $name;//Declare a variable const SKILLS = 'Kazaki';//Declare a constant public function run(){ echo 'People in the tower'; } } $person = new Person(); $person->name = 'Yasuo'; echo '<br>'; $person->run(); echo '<br>'; echo 'I am'.$person->name; echo '<br>'; echo 'My skills are:'.$person::SKILLS;

The function and method of php object-oriented constructor

Constructor is a special method. It is mainly used to initialize objects when creating objects, that is, to assign initial values to object member variables. It is always used in the statement of creating objects together with the new operator. A special class can have multiple constructors, which can be distinguished according to the number of parameters or the type of parameters.

Constructor is used to initialize objects. This method can have no parameters or multiple parameters. Defining a constructor is also very simple. It's worth noting that the function construct is preceded by two underscores

<?php class Person{ public $name; public $height; function __construct($name,$height){ $this->name = $name; $this->height = $height; } public function play(){ if($this->height>175){ return $this->name.'Can play basketball'; }else{ return $this->name.'Can't play basketball'; } } } $person = new Person('cyy','168'); echo $person->play();

The constructor is used to initialize objects. If there is no constructor, PHP will automatically generate one. The auto generated constructor has no parameters and no operations.

The function and usage of php object-oriented destructor

Destructors are the opposite of constructors. They are called when an object is destroyed to free memory. The format of defining a destructor is: "destruct(), which, like a constructor, is preceded by two underscores" ".

<?php class Person{ public $name; public $height; function __construct($name,$height){ $this->name = $name; $this->height = $height; } function __destruct(){ echo 'Object destroyed'; } public function play(){ if($this->height>175){ return $this->name.'Can play basketball'; }else{ return $this->name.'Can't play basketball'; } } } $person = new Person('cyy','168'); echo $person->play(); echo '<br>';

Precautions for use of destructors:

php uses a "garbage collection" mechanism to automatically clear objects that are no longer used and free memory. That is to say, even if unset function is not used, the destructor will be called automatically. Here is just to make clear when the destructor is called. In general, there is no need to create the destructor manually.

15 May 2020, 05:16 | Views: 1343

Add new comment

For adding a comment, please log in
or create account

0 comments