Chapter 3 (advanced oop)-php and mysqlweb development, php and mysql programming-php Tutorial

Source: Internet
Author: User
Chapter 3 (advanced oop)-full-read object cloning in php and mysqlweb development and mysql programming

Objects are passed by reference by default. if you need to copy an object and perform a complete cp for the object value, you need to use the clone method.

Class Corporate_Drone {private $ employeeid; private $ tiecolor; function setEmployeeID ($ employeeid) {$ this-> employeeid = $ employeeid;} function getEmployeeID () {return $ this-> employeeid;} function setTieColor ($ tiecolor) {$ this-> tiecolor = $ tiecolor;} function getTieColor () {return $ this-> tiecolor ;}} $ drone1 = new Corporate_Drone (); $ drone1-> setemployee ID ("12345"); $ drone1-> setTieColor ("red"); $ drone2 = clone $ drone1; // clone the instance $ drone1 here. because $ drone1 has already set attributes, the instance is copied at the moment, $ drone2-> setEmployeeID ("67890 "); // The cloned $ drone2 set's own attribute echo "Drone1 employeeID is ". $ drone1-> getEmployeeID (). "\ n"; echo "Drone1 tie color is ". $ drone1-> getTieColor (). "\ n"; echo "Drone2 employeeid is ". $ drone2-> getEmployeeID (). "\ n"; echo "Drone2 tie color is ". $ drone2-> getTieColor (). "\ n"; ---- Drone1 employeeID is 12345Drone1 tie color is red Drone2 employeeid is 67890 // This is modified, however, the color below is still the instance $ drone1 attribute value Drone2 tie color is red

The above is to clone an entire object. you can use the _ clone method to adjust the behavior of object cloning. this method is only executed during the cloning operation.

Class Corporate_Drone {private $ employeeid; private $ tiecolor; function setEmployeeID ($ employeeid) {$ this-> employeeid = $ employeeid;} function getEmployeeID () {return $ this-> employeeid;} function setTieColor ($ tiecolor) {$ this-> tiecolor = $ tiecolor;} function getTieColor () {return $ this-> tiecolor ;} function _ clone () {$ this-> tiecolor = "blue"; // here, this _ clone method modifies the color tiecolor during cloning} $ drone1 = New maid (); $ drone1-> setEmployeeID ("12345"); $ drone1-> setTieColor ("red"); $ drone2 = clone $ drone1; // clone the instance $ drone1 here. because $ drone1 has already set attributes, the instance is copied at the moment, $ drone2-> setEmployeeID ("67890 "); // The cloned $ drone2 set's own attribute echo "Drone1 employeeID is ". $ drone1-> getEmployeeID (). "\ n"; echo "Drone1 tie color is ". $ drone1-> getTieColor (). "\ n"; echo "Drone2 employeeid is ". $ drone2-> getEmployeeID (). "\ n"; echo "Dro Ne2 tie color is ". $ drone2-> getTieColor (). "\ n"; ---- Drone1 employeeID is 12345Drone1 tie color is redDrone2 employeeid is 67890Drone2 tie color is blue // The color is changed.
Class inheritance

Inheritance is the relationship between the father and the son. the son can use his father's sports car, or buy his own bicycle while driving his father's sports car. after all, they all belong to the family.

Class Employee {private $ name; function setName ($ name) {if ($ name = "") echo "Name connot be blank! "; Else $ this-> name = $ name;} function getName () {return" My name is ". $ this-> name. "\ n" ;}} class Executive extends Employee {// here extends is used for inheritance. Executive inherits all attributes and methods of the Employee class. function pillageCompany () {// Here, Executive also has its own method echo "Im selling company assets to finance my yacht! ";}}$ Exec = new Executive (); $ exec-> setName (" richard "); // because the Employee parent class is inherited, so you can setnameecho $ exec-> getName (). "\ n"; // same as $ exec-> pillageCompany (); // You can also use your own method ---- My name is richardIm selling company assets to finance my yacht!
Inheritance and constructor

If the parent class has a constructor and the child class does not have a constructor, the constructor of the parent class will be executed when the child class is instantiated, if both the parent class and child class have constructors, only the child class constructor will be executed when the child class is instantiated, unless

Parent explicitly calls the parent class constructor, or directly calls the constructor classname :__ construct () of the parent class ()

Static inheritance and latency binding

In one case, a parent method needs to interact with static class attributes, but these static class attributes may be overwritten in the subclass. how can we define the scope of the static class, I have never said this before, because the static class definition exists only in the local function domain, but its value is not lost when the program execution leaves this scope.

This problem is solved after php5.3.

Class Employee {public static $ favSport = "Football"; // static variable $ favSport public static function watchTV () {echo "Watching ". self: $ favSport; // The static variable of class employee is called here} class Executive extends Employee {public static $ favSport = "polo "; // static variable $ favSport in class Excutive. because Ex inherits Em, it can overwrite the variable, but because the static method watchTV is executed, the self keyword determines its scope at Compilation rather than runtime. Therefore, the result is Football public static function watchTV () {// if you rewrite the watchTV function and redefine the static variable, you can redefine the static scope to achieve the desired coverage echo "Watching ". static: $ favSport;} echo Executive: watchTV ();
Interfaces and abstract classes

The interface defines the general specification for implementing a certain service, declares required functions and constants, but does not specify how to implement them. The key is to establish a set of general principles that must be implemented. this interface can be implemented only when these principles are met.

An abstract class is a class that cannot be instantiated and can only be used as the base class inherited by other classes. for example, a class named media is used to describe the common nature of various public Publications, because media does not represent real entities, but generalized representation of similar entities, it is not instantiated. in this way, it needs to be declared as an abstract class and then inherited from this abstract class by a variety of derived Media classes.

When to use interfaces and when to use abstract classes
  • If you want to create a model that will be referenced by closely related objects, you can use abstract classes. if you want to create functions that will be used by objects you do not want to close, you should use interfaces.
  • If the behavior must be inherited from multiple sources, an interface is used.
  • If you know that all classes share a public behavior implementation, use an abstract class and implement the behavior in it. Interface cannot be implemented.
Namespace

What is a namespace? In a broad sense, namespace is a way to encapsulate things. This abstract concept can be seen in many places. For example, a directory in the operating system is used to group related files. for files in the directory, it plays the role of namespace. For example, the file foo.txt can exist in the/home/greg and/home/other directories at the same time, but there cannot be two foo.txt files in the same directory. In addition, when accessing the foo.txt file outside the/home/greg Directory, we must put the directory name and directory separator before the file name to get/home/greg/foo.txt. This principle is applied to the field of programming as a namespace concept.

<? Phpnamespace Foo \ Bar; include 'file1. php '; const FOO = 2; function foo () {} class foo {static function staticmethod () {}}/* non-qualified name */foo (); // Parse it to Foo \ Bar \ foo resolves to function Foo \ Bar \ foofoo: staticmethod (); // resolve it to the static method staticmethod of the class Foo \ Bar \ foo. Resolves to class Foo \ Bar \ foo, method staticmethodecho FOO; // resolves to constant Foo \ Bar \ FOO/* qualified name */subnamespace \ foo (); // Parse it to function Foo \ Bar \ subnamespace \ foosubnamespace \ foo: staticmethod (); // resolve it to class Foo \ Bar \ subnamespace \ foo, // and the class method staticmethodecho subnamespace \ FOO; // The constant Foo \ Bar \ subnamespace \ FOO/* fully qualified name */\ Foo \ Bar \ foo (); // Parse it to function Foo \ Bar \ foo \ Bar \ Foo: staticmethod (); // resolve it to class foo \ Bar \ Foo And the class method staticmethodecho \ Foo \ Bar \ FOO; // parses it as the constant Foo \ Bar \ FOO?>

This article was created by Peter yuan and is licensed on the Chinese mainland using the signature-non-commercial use 2.5. You need to contact the author before reprinting and referencing, and sign the author and indicate the source of the article. God-like teenagers» Chapter 3 (advanced oop)-parallel reading in php and mysql web development and mysql programming

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.