PHP class and object full parsing (i) _php tutorial

Source: Internet
Author: User

Full parsing of PHP classes and objects (i)


1. Class and Object object: an individual that actually exists in every object in the class. $a =new User (); A $ A reference to the instantiation: PHP aliases, two different variable names point to the same content encapsulation: The object's properties and methods are organized in a class (logical unit) to inherit: A new class is created based on the original class, thus the code is reused; Polymorphic: Allows pointers of subclass types to be assigned to a pointer of the parent class type. -------------------------------------2. Automatic loading of objects: automatic loading by defining special __autoload functions, this function is called automatically when references to classes that are not defined in the script. 1 [PHP] View plaincopyprint?2 function __autoload ($class) {3 require_once ("classes/$class. class.php"); 4} Why use __ AutoLoad 1, first of all do not know where this class of files stored in the place, 2, the other is not know when to use this file. 3, especially when the project file is very long, it is impossible to write the require in the beginning section of each file. Replaced by a require_once ("classes/books.class.php"); Require_once ("classes/employees.class.php"); Require_once ("classes/events.class.php"); Require_once ("classes/patrons.class.php"); Zend recommends one of the most popular ways to include a path in the file name. For example, the following example: 1 [php] View plaincopyprint?2 3 view Sourceprint? 4//Main.class 5 6 function __autoload ($class _name) {7 $path = Str_replace (' _ ', Directory_separator, $class _name); 8 req Uire_once $path. " PHP '; 9} temp = new Main_super_class (); All underscores are replaced with delimiters in the path, and the main/super/class.php file is removed from the previous example. Disadvantage: In the coding process, it is necessary to know clearly where the code file should be located, andAnd because the file path is hard-coded in the class name, if you need to modify the structure of the folder, we must manually modify all the class names. Using the ' Include all ' method is convenient if you are in a development environment and are not very concerned about speed. By placing all the class files in one or several specific folders, and then looking through the load. For example Another way is to create an associated configuration file between the class file and his location, for example: View Sourceprint? configuration.php array_of_associations = Array (' mainsuperclass ' = ' c:/main/super/class.php ', ' MainPoorClass ' = ' C:/ Blablabla/gy.php '); The called file ------------------------------------------------3. Constructors and Destructors PHP construction Method __construct () allows the constructor to be executed before instantiating a class. The constructor method is a special method in the class. When you use the new operator to create an instance of a class, the constructor method is automatically called, and its name must be __construct (). (Only one construction method can be declared in a class, but the constructor is called every time the object is created, and it cannot be invoked actively, so it is usually used to perform some useful initialization tasks.) The method has no return value. Function: the constructor parent::__construct () that initializes the object subclass to perform the classification when the object is created. destructor: __destruct () Definition: Special inner member function, no return type, no parameters, no arbitrary invocation, no overloading; The system automatically calls the resource allocated in the constructor when the class object is at the end of its life. And the constructor method corresponds to the Destructor method, the destructor allows some operations to be performed before destroying a class or to perform some functions, such as closing a file, releasing a result set, and so on. Destructors cannot have any arguments, and their names must be __destruct (). Role: Cleans up the aftermath, for example, using new to open up a memory space when creating an object, use a destructor to release the resources allocated in the constructor before exiting. Example: Class Person {public $name; public $age;//Define a constructor method to initialize the assignment public function __construct ($name, $age) {$this->name= $na Me $this->age= $age; Public function say () {echo ' My name is: '. $this->name. '
"; echo "My Age is:". $this->age;} destructor function __destruct () {echo "Goodbye:". $this->name;}} $p 1=new person ("Ren", 25); $p 1->say (); ---------------------------------------------------------------4. Access Control access control to a property or method by adding the keyword public, protected, or The class member defined by the private implementation of public can be accessed anywhere, and the class member defined by protected can be accessed by the subclass and parent of the class in which it resides (of course, the class where the member resides), and the class member of the private definition can only be accessed by the class in which it resides. Access control class Members for class members must use the keyword public, protected, or private to define methods in the access control class of a method that must be defined using the keyword public, protected, or private. If these keywords are not set, the method is set to the default public. Example: Class MyClass {public $public = ' public '; protected $protected = ' protected '; private $private = ' private '; function Printhello () {echo $this->public; echo $this->protected; echo $this->private;}} $obj = new MyClass (); Echo $obj->public; This line can be performed normally echo $obj->protected; This guild produces a fatal error. Echo $obj->private; This line will also produce a fatal error $obj->printhello (); Output public, Protected, and Private-------------------------------------------------------------5. Object inheritance Inheritance definition:Create a new class based on the original class, and the purpose of code reuse --------------------------------------Overwrite is an overload that is used when object inheritance is a method with different parameters of the same method name in a single object-------------------------------------- Inheritance has been well-known for a programming feature, and the PHP object model also uses inheritance. Inheritance will affect the relationship between classes and classes, objects and objects. For example, when you extend a class, subclasses inherit all the public and protected methods of the parent class. However, the methods of the subclass override the methods of the parent class. Inheritance is very useful for the design and abstraction of features, and adding new functionality to similar objects eliminates the need to re-write these common features. Class Person {public $name, public $age, function say () {echo "My name is:". $this->name.
"; echo "My Age is:". $this->age;}} Class Student extends person {var $school;//Student's School's attribute function study () {echo "My name is:". $this->name. "
"; echo "My shool is:". $this->school;}} $t 1 = new Student (); $t 1->name = "Zhangsan"; $t 1->school = "Beijindaxue"; $t 1->study (); --------------------------------------------6. Scope resolution operator (::) Scope resolution operator (also known as Paamayim Nekudotayim) or, more simply, a pair of colons, Can be used to access static members, methods, and constants, and can also be used to override members and methods in a class. When you access these static members, methods, and constants outside of the class, you must use the name of the class. The two special keywords, self and parent, are used to access members or methods within a class. Note: When a subclass overrides a method in its parent class, PHP no longer executes methods that have been overridden in the parent class until these methods are called in the subclass example:

http://www.bkjia.com/PHPjc/934463.html www.bkjia.com true http://www.bkjia.com/PHPjc/934463.html techarticle full parsing of PHP classes and objects (i) 1. Class and Object object: an individual that actually exists in every object in the class. $a =new User (); A $ A quote after instantiation: PHP aliases, two different ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.