Basic object-oriented basic concept instance analysis in the PHP getting started tutorial.
This article describes the basic concepts of PHP object-oriented. We will share this with you for your reference. The details are as follows:
Demo1.php
<? Php // how to create a class format: modifier class name {} // we will create a computer class, this class can create objects (production computer) class Computer {// The first letter of the class name in uppercase} // create a Computer, that is, the object declaration // format: Variable = new class Name (); // new Compuer () indicates the instantiation process (meaning creating an object) // $ compuer1 = new Compuer () in this process, the address of the instantiated object is given to $ compuer1 // $ compuer1, which can be called the application of the Object $ computer1 = new Computer (); // This is the first computer we created $ computer2 = $ computer1; // This is the second computer we created var_dump ($ computer1); echo '<br/> '; var_dump ($ computer1);?>
Demo2.php
<? Php header ('content-Type: text/html; charset = UTF-8; '); // class Computer {// declaration format of field members: modifier variable name [= xxx]; // public $ _ name = 'lenovo '; // public indicates a total, you can access // public $ _ model = 'i7 'outside the class; //} // create an object, generate a Computer-> indicates pointing to // $ computer1 = new Computer (); // echo $ computer1-> _ name; // $ computer1-> _ name = 'Dell '; // echo $ computer1-> _ name; class Computer {// The Declaration format of field members: modifier variable name [= xxx]; public $ _ name; // public indicates a total Access public $ _ model;} // create an object and generate a Computer.-> indicates pointing to $ computer1 = new Computer (); // assign a value to the member field $ computer1-> _ name = 'lenovo '; // value: echo $ computer1-> _ name;?>
Demo3.php
<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {public $ _ name; // public indicates a total, you can access public $ _ model outside the class; // method creation format: modifier function Method Name () {}// if no modifier is added, the default value is public function _ run () {echo 'method I am running ';} // creates an object, generate a Computer-> indicates to point to $ computer1 = new Computer (); $ computer1-> _ run ();?>
Demo4.php
<? Php header ('content-Type: text/html; charset = UTF-8; '); // class Computer {// declaration format of field members: modifier variable name [= xxx]; // public $ _ name = 'lenovo '; // public indicates a total, you can access // public $ _ model = 'i7 'outside the class; //} // create an object, generate a Computer-> indicates pointing to // $ computer1 = new Computer (); // echo $ computer1-> _ name; // $ computer1-> _ name = 'Dell '; // echo $ computer1-> _ name; class Computer {// The Declaration format of field members: modifier variable name [= xxx]; public $ _ name; // public indicates a total Access public $ _ model;} // create an object and generate a Computer.-> indicates pointing to $ computer1 = new Computer (); // assign a value to the member field $ computer1-> _ name = 'lenovo '; // value // echo $ computer1-> _ name; $ computer2 = $ computer1; echo $ computer2-> _ name;?>
Demo5.php
<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {public $ _ name; // public indicates a total, you can access public $ _ model outside the class; // method creation format: modifier function Method Name () {}// if no modifier is added, the default value is public function _ run ($ _ who) {echo $ _ who. 'is the running method';} // create an object and generate a Computer.-> indicates pointing to $ computer1 = new Computer (); $ computer1-> _ run (one-stop website creation);?>
Demo6.php
<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {// create a constructor public function Computer () {echo 'I am constructor';} // you can run the constructor as long as it is instantiated. // $ computer = new Computer ();?>
Demo7.php
<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {// create a constructor public function _ construct () {echo 'I am a more advanced constructor';} // you can run the constructor as long as it is instantiated. // $ computer = new Computer (); new Computer () ;?>
Demo8.php
<? Php header ('content-Type: text/html; charset = UTF-8; '); class Computer {// create a constructor public function _ construct () {echo 'I am a more advanced constructor';} // destructor public function _ destruct () {echo 'I am a constructor ';} // common method public function _ run () {echo 'I am a common method';} // you can run the constructor $ computer = new Computer () as long as it is instantiated (); $ computer-> _ run ();?>