PHP object oriented study notes one of the basic concepts

  • 2020-05-24 05:18:19
  • OfStack

1 > if("false") is equivalent to if(true), because the non-empty string is true
2 > Check 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 base 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 > class son extends parent{}
To call methods of the parent class, such as the constructor, use parent:: s 52en ();

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 class, abstract method
abstract class xxx{
...
abstract function write (); / / not {}
}

A subclass of an abstract class redeclares and implements a method. Access control for a newly implemented method cannot be more stringent than that for an abstract method.

8 > Interface interface
An interface can contain property and method declarations, but the body of the method is empty.
For example: interface a{
public function b();
}
Any class that implements the 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 > Abnormal exception
PHP5 introduces exception classes

10 > The interceptor interceptor
__get ($property); Called when accessing an undefined property
__set ($property, $value); Is called when assigning a value to an undefined property
__isset ($property); Called when isset() is used for an undefined property;
__unset ($property); Called when unset() is called on an undefined property;
__call ($method, $arg_array); Called when calling an undefined method
Example: the implementation of s 150en ()
 
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 > Destructor method s s 157en ()

12 > __clone (); The difference with 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 passing of objects are both references.
To copy, use: $second= clone $first; // now $first and $second are two completely different objects,(by_value copy)
If you want to control replication, you have to implement 1 special method s s 190en ()

13 > Automatic loading: s = s = s = s = s = s
PHP5 introduces the s 198en () interceptor method to automatically include class files. When PHP encounters an attempt to instantiate an unknown class, it will try to call s 200en () method and pass the class name as a string argument to it.
For example, a very simple automatic location and inclusion policy:
function __autoload( $classname){
includ_once "$classname.php";
}

====================
14 > Use a string to reference a class dynamically
 
$classname="Task"; 
require_once("tasks/{$classname}.php); 
$myObj= new $classname(); 
$method="getTitle"; 
$myObj->$method(); // Dynamic methods  

15 > Class and object functions
 
class_exist(); // Check if the class exists  
get_declared_classes(); // Gets all the classes defined in the current script process (array returns ) 
get_class_methods();// class public Methods list (array) 
method_exist($objname,$method); // Object or class method exists  
is_callable();// Object or class methods do not only exist , And access to the  
get_class_vars(); //  attribute  
get_parent_class( Class or object name ); // The parent class  
is_subclass_of(); // If a subclass , Regardless of the interface , Interface with  instanceof The operator  

16 > Reflection API
It consists of a series 1 of built-in classes that can analyze properties, methods, classes, and parameters, dynamically retrieve information, and dynamically invoke methods.

Related articles: