Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The basic concept of PHP object-oriented

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the basic concept of PHP object-oriented". In the daily operation, I believe that many people have doubts about the basic concept of PHP object-oriented. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "basic concept of PHP object-oriented". Next, please follow the editor to study!

1 > if ("false") is equivalent to if (true) because the non-empty string is true

2 > check the data type:

Is_array ()

Is_object ()

Is_string ()

Is_null ()

Is_integer ()

3 > PHP5 introduces a class type hint (type hint) to constrain the parameter type of a method (not the basic data type, but the class): put the class name before the method parameter that needs to be constrained.

For example: function write (ShopProduct $shopProduct) {}

4 > instanceof operator: if the object of the left Operand is of the type shown in the right Operand, the result is true

For example: if ($shopProduct instanceof BookProduct) {}

5 > inherit class son extends parent {}

To call a method of the parent class, such as a constructor, use parent::__construct ()

6 > static methods and properties

Class StaticExample {

Static public $a

Static public function hello () {}

}

External access uses:

For example: print StaticExample::$a

Internal access using self::

For example: self::$a

7 > Abstract classes, abstract methods

Abstract class xxx {

...

Abstract function write (); / / No {}

}

A subclass of an abstract class redeclares the method and implements it. The access control of the newly implemented method cannot be more stringent than that of the abstract method.

8 > Interface interface

Only functions are defined, no implementation is included. The interface can contain property and method declarations, but the method body is empty

For example: interface a {

Public function b ()

}

Any class that implements an interface must implement all the methods defined in the interface, otherwise it must be an abstract class.

Class uses implements in the declaration to implement an interface.

Class Shop implements a {

Public function b () {

...

}

}

9 > exception exception

PHP5 introduces exception classes

10 > interceptor interceptor

_ _ get ($property); called when accessing undefined properties

_ _ set ($property,$value); called when assigning values to undefined attributes

_ _ isset ($property); called when using isset () for undefined attributes

_ _ unset ($property); called when calling unset () on an undefined attribute

_ _ call ($method, $arg_array); called when an undefined method is called

Example: implementation of _ _ get ()

The copy code is as follows:

Function _ _ get ($property) {

$method= "get {$property}"

If (method_exists ($this,$method)) {

Return $this- > $method ()

}

}

Function getName () {return "Bob";}

Function _ _ isset ($property) {

$method= "get {$porperty}"

Return (method_exists ($this, $method))

}

Function _ _ set ($property, $value) {

$method= "set {$property}"

If (method_exists ($this,$method)) {

Return $this- > $method ($value)

}

}

11 > destructing method _ _ destruct ()

12 > _ _ clone (); difference from the clone keyword

Class CopyMe ()

$first= new CopyMe ()

$second=$first

/ / PHP4: $first and $second are two completely different objects

/ / PHP5: $first and $second point to the same object

In PHP5, the assignment and transfer of objects are references.

If you want to copy, use: $second= clone $first; / / now $first and $second are two completely different objects, (by_value copy)

If you want to control replication, implement a special method _ _ clone ()

13 > Auto load: _ _ autoload ()

PHP5 introduces the _ _ autoload () interceptor method to automatically include class files. When PHP encounters an attempt to instantiate an unknown class, it attempts to call the _ _ autoload () method and pass it the class name as a string argument.

For example, a very simple automatic positioning and inclusion strategy:

Function _ _ autoload ($classname) {

Includ_once "$classname.php"

}

=

14 > using string dynamic reference classes

The copy code is as follows:

$classname= "Task"

Require_once ("tasks/ {$classname} .php)

$myObj= new $classname ()

$method= "getTitle"

$myObj- > $method (); / / dynamic method

15 > Class functions and object functions

The copy code is as follows:

Class_exist (); / / check whether the class exists

Get_declared_classes (); / / get all the classes defined in the current script process (returned as array)

Get_class_methods (); / / list of all public methods in the class (array)

Method_exist ($objname,$method); / / whether a method of an object or class exists

Methods of is_callable (); / / objects or classes not only exist, but can also be accessed

Get_class_vars (); / / attribute

Get_parent_class (class or object name); / / parent class

Is_subclass_of (); / / whether it is a subclass, regardless of the interface, the interface uses the instanceof operator

16 > reflection API

It is composed of a series of built-in classes that can analyze properties, methods, classes and parameters, and can obtain information and call methods dynamically.

At this point, the study of "PHP object-oriented basic concepts" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report