php core technology and the best time --- oop base

<?php
/**
 * Created by PhpStorm.
 * User: cl
 * Date: 2019/8/12
 * Time: 7:08
 */
/*oop*/
class Person{
    public $name;
    public $gender;
    public function say(){
        echo $this->name,'is',$this->gender;
    }
}

$student = new Person();
$student->name = "CL";
$student->gender = "MAN";
$student->say ();
 // CLisMAN 
var_dump (( Array ) $ Student );
 // Array (2) {[ "name"] => String (2) "CL" [ "Gender"] => String (. 3) "MAN "} 
// attributes of the object, property of an object is the key to distinguish it from another object. Because of php objects using arrays to simulate the 
// so we turn the object into an array, you can see the object properties owned by the. 
// here, we can visually recognize that the object is a pile of data. That being the case, an object can be stored for use when needed, that is serialized object. 
STR $ = the serialize ( $ Student );
 var_dump ( $ STR );
 // String (60) "O:. 6:" the Person ": 2: {S:. 4:" name "; S: 2:" CL "; S : 6: "gender"; s : 3: "MAN";} "
// Object (the Person) # 2 (2) {[ "name"] => String (2) "CL" [ "Gender"] => String (. 3) "MAN"} 
// can be seen, the target sequence after storing only the object's properties. Class is composed of properties and methods, and the object is a collection of attributes, generated by the different objects in the same class, 
// each have different properties, but sharing the class code space code in the method area. 

/ * Object array * / 
// array is composed of key data, the key array and object attribute / attribute value pairs are very similar. After the target sequence and the result is an array of sequence 
// surprisingly similar 
$ student_arr = [ 'name' => 'CL', 'Gender' => 'MAN' ];
 var_dump ( the serialize ( $ student_arr ));
 / /   String (49) "A: 2: {S:. 4:" name "; S: 2:" CL "; S:. 6:" Gender "; S:. 3:" MAN ";}" 
// except that: the object further comprises a pointer to the class to which it belongs. 

/ * Object class * / 
// If the object also contains objects, after serialization look like? 
class Family{
    public $people;
    public $location;
    public function __construct ($p,$loc){
        $this->people = $p;
        $this->location = $loc;
    }
/*    public function __destruct(){
        var_dump('魔术方法之析构方法');
    }*/
}
$tom = new Family($student,"peking");
var_dump(serialize($tom));
//




 * Syntactic sugar are those that do not add new features to the computer, but for humans even sweeter syntax 
 magic method __construct method * Family class is a standard. The magic method, also known as the constructor. When the class has a constructor will be created each time the object 
 * call this method, it is ideal to do some initialization before using an object, such as: assigns them connect to the database, etc. 
 * There constructors have destructors, namely destruct method, this method references are deleted so an object, or when the object is explicitly destroyed. 
 * These two methods are the most common and most useful magic method 
 * / 
/ * the SET and magic methods of GET * / 
class the Account {
     Private  $ the User = 1 ;
     Private  $ pwd = 2 ; 

    public  function __get ( $ name ) {
         echo 'request'. $ name ; 
    } 
    public  function the __set ( $ name , $ Val) {
         Echo "setting request." $ Name "is.". $ Val ; 
    } 


    public  function the __call ( $ name , $ arguments ) {
         // $ name: name of the method to be invoked 
        // $ arguments: array, should contain parameters passed to the method 
        var_dump ( $ name );
         var_dump ( $ arguments ); 
    } 
    public  static  function __callStatic ( $ name , $ arguments ) {
         var_dump ( $ name );
         var_dump ( $ arguments ); 
    }
    public  function the __toString () 
    { 
        return 'any string' ; 
    } 

} 
$ A = new new the Account ();
 $ A -> User; // if no error occurs __get magic methods, generally meaning that the object can not access a private property 
// request User 
$ a -> name = '123'; // because the object name attribute is not, it will trigger the magic method __set 
// name request set to 123 
/ * 
 * can visually see if a class is defined to set and get the magic method, then when assigned to an object or attribute value, even if 
 * this property does not exist, no error will enhance the robustness of the program to some extent 
 * / 
// Well, if prevent calling when a method is not accessible (if not defined, or not visible), call () is called. 
A $ -> Demo ( '. 1', '2' );
 // "Demo"
Array // (2) {[0] => String (. 1) ". 1" [. 1] => String (. 1) "2"} 
// when a static method call absence or insufficient permissions, callStatic () will is called 
the Account :: Demo ();
 // "Demo" 
// Array (0) {} 

// of course, using magic method "preventing the given call does not exist," magic method was not intended. In fact, the magic method to dynamically create a method becomes possible. 
// This is useful grammar and other MVC framework design. This method can be carried out using callStatic magic methods through a piece of code to dynamically create and late binding 

// toString method 
echo  $ A ; // arbitrary string 
// such as when printing an object, the object has to see which attributes it What value, if defined toString method, will be able to test, echo print object body 
// object will automatically call the toString method which belongs to the class definition, formatting the output data of the object contains. Without this method, the object will echo one pair error

 

Guess you like

Origin www.cnblogs.com/cl94/p/11343616.html