PHP object-oriented basics _ PHP Tutorial

Source: Internet
Author: User
PHP object-oriented basics. I recently participated in several PHP engineers for interviews, but the answers to the pen questions were not satisfactory. I came back and summarized the cause of the failure. I did not read the PHP Manual. The PHP basic interview questions of several companies can be recently interviewed by several PHP engineers, but the answers to the questions are not satisfactory. I came back and summarized the cause of the failure. I did not read the PHP Manual. The PHP basic questions of several companies can be found in the PHP Manual. Ah, now I know that the best interview book is The PHP Manual.
The following is an excerpt from the basic PHP object-oriented knowledge, which is from the PHP5.1 manual.
1. the variable members of a class are called "attributes", "fields", and "features". in this document, they are collectively referred to as "attributes ".
2. the variables in the property can be initialized, but the initialization value must be a constant. here the constant refers to the php script being a constant during the compilation phase, rather
The constant calculated in the running phase after the compilation phase.
3. in the Member methods of the class, you can use $ this-> property (property is the property name) to define the attributes and methods of the class,
You can use self ::$ property instead of static attributes of the category or static methods.
4. you can use the pseudo variable $ this in the non-static method of the class. this pseudo variable is the reference of the instantiated object that calls this method.
5. the constant value must be a fixed value and cannot be modified. it cannot be the result of a variable, a class attribute, or other operations (such as a function call.
Class MyClass
{
Const constant = 'constant value ';
Function showConstant (){
Echo self: constant. "\ n ";
}
}
Echo MyClass: constant. "\ n ";
$ N = new MyClass ();
$ N-> showConstant ();
?>
6. The constructor class calls this method every time it creates an object, so it is very suitable for initialization before using the object.
If the constructor is defined in the subclass, the constructor of its parent class will not be called secretly. To execute the constructor of the parent class, it must be in the constructor of the subclass.
Call parent ::__ construct ().
7. the destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed.
The destructor of the parent class will not be called by the engine secretly. To execute the destructor of the parent class, you must explicitly call the Destructor in the subclass.
Parent: :__ destruct ().
The Destructor is called when the script is disabled. all header information is sent.
Throwing an exception in the destructor will cause a fatal error.
8. when a class is extended, the subclass inherits all the public and protection methods of the parent class. However, the subclass method overwrites the method of the parent class.
9. The range resolution operator (: :) can be used to access static members, methods, and constants.
When you access these static members, methods, and constants outside the class, you must use the class name.
The special keywords self and parent are used to access members or methods within the class.
10. when a subclass overwrites the methods in its parent class, PHP will no longer execute the override methods in its parent class until these methods are called in the subclass. This
Mechanisms also apply to constructor and Destructor, overload, and magic functions.
11. static variables and methods
If the declared class member or method is static, you can directly access it without instantiating the class. A static member cannot be accessed through an object (static method
Except ).
Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods.
Static attributes cannot be accessed by objects through the-> operator.
Use: when a non-static method is called, an E_STRICT error occurs.
Like all other PHP static variables, static attributes can only be initialized as a character value or a constant, and expressions cannot be used. So you can
To initialize a static property as an integer or array, but cannot point to another variable or function return value or point to an object.

12. if "visibility" is not specified, attributes and methods are considered public.
13. abstract class
An abstract class cannot be instantiated directly. you must first inherit this abstract class and then instantiate it. An abstract class must contain at least one abstract method. If
Class methods are declared as abstract, so they cannot include specific function implementations.
When inheriting an abstract class, the subclass must implement all abstract methods in the abstract class. In addition, the visibility of these methods must be the same as that in the abstract class (
Or loose ). If an abstract method in an abstract class is declared as protected, the method implemented in the subclass should be declared as protected.
Or public, but cannot be defined as private.
Application example:
Abstract class AbstractClass
{
// Force the subclass to define these methods
Abstract protected function getValue ();
Abstract protected function prefixValue ($ prefix );
// Common method (non-abstract method)
Public function printOut (){
Print $ this-> getValue (). "\ n ";
}
}
Class ConcreteClass1 extends AbstractClass
{
Protected function getValue (){
Return "ConcreteClass1 ";
}
Public function prefixValue ($ prefix ){
Return "{$ prefix} ConcreteClass1 ";
}
}
14. Using interfaces, you can specify the methods that a class must implement, but you do not need to define the specific content of these methods.
We can use interfaces to define an interface, just like defining a standard class, but defining all methods is empty.
All methods defined in the interface must be public, which is a feature of the interface.
To implement an interface, you can use the implements operator. Class must implement all methods defined in the interface, otherwise a fatal error will be reported.
To implement multiple interfaces, you can use commas to separate the names of multiple interfaces.
When multiple interfaces are implemented, the methods in the interface cannot have duplicate names.
Interfaces can also be inherited by using the extends operator.
Constants can also be defined in the interface. The use of interface constants and class constants is identical. They are all set values and cannot be modified by the quilt class or subinterface.
Application example:
// Interface Definition
Interface iTemplate
{
Public function setVariable ($ name, $ var );
Public function getHtml ($ template );
}
// Use the interface
Class Template implements iTemplate
{
Private $ vars = array ();

Public function setVariable ($ name, $ var)
{
$ This-> vars [$ name] = $ var;
}

Public function getHtml ($ template)
{
Foreach ($ this-> vars as $ name => $ value ){
$ Template = str_replace ('{'. $ name. '}', $ value, $ template );
}

Return $ template;
}
}
15. PHP5 provides an iteration object function. just like using arrays, you can use foreach to traverse the attributes of an object.
By default, only externally visible attribute values can be obtained in an external iteration.
Application example:
Class MyClass
{
Public $ var1 = 'value 1 ';
Public $ var2 = 'value 2 ';
Public $ var3 = 'value 3 ';
Protected $ protected = 'protected var ';
Private $ private = 'private var ';
Function iterateVisible (){
Echo "MyClass: iterateVisible: \ n ";
Foreach ($ this as $ key => $ value ){
Print "$ key => $ value \ n ";
}
}
}
$ Class = new MyClass ();
Foreach ($ class as $ key => $ value ){
Print "$ key => $ value \ n ";
}
Echo "\ n ";

$ Class-> iterateVisible ();
?>
16. design mode
Factory allows you to instantiate an object during code execution. It is called the factory model because it is responsible for "production" objects. Factory
The method parameter is the class name corresponding to the object you want to generate.
Singleton is used to generate a unique object for a class. Database Connection is the most commonly used place. Generate
Object.
Application example:
Class Example
{
// Save the class instance in this attribute
Private static $ instance;

// The constructor is declared as private to prevent direct object creation.
Private function _ construct ()
{
Echo 'I am constructed ';
}
// Singleton method
Public static function singleton ()
{
If (! Isset (self: $ instance )){
$ C = _ CLASS __;
Self: $ instance = new $ c;
}
Return self: $ instance;
}

// Common methods in the Example class
Public function bark ()
{
Echo 'Woof! ';
}
// Prevents users from copying object instances
Public function _ clone ()
{
Trigger_error ('Clone is not allowed. ', E_USER_ERROR );
}
}
?>

In this way, we can get a unique Example class object.

// This write method will fail because the constructor is declared as private.
$ Test = new Example;
// The following is a singleton object of the Example class.
$ Test = Example: singleton ();
$ Test-> bark ();
// Copying an object will result in an E_USER_ERROR.
$ Test_clone = clone $ test;
?>
17. PHP 5 adds a final keyword. If the method in the parent class is declared as final, the subclass cannot overwrite the method. if a class is
If it is declared as final, it cannot be inherited.
18. object replication can be completed by using the clone keyword (if the object contains the _ clone () method, it will be called first ). _ Clone () in the object ()
Methods cannot be called directly.
$ Copy_of_object = clone $ object;
After an object is copied, PHP5 performs a shallow copy operation on all attributes of the object ). References in all properties are still not
Point to the original variable. If the _ clone () method is defined, the _ clone () method in the newly created object (copying the generated object) will be called
Can be used to modify the attribute value (if necessary ).

19. object comparison
When two object variables are compared using the comparison operator (=), the comparison principle is: if the attributes and attribute values of the two objects are equal, and the two objects
Is an instance of the same class, then the two object variables are equal. Www.2cto.com
If the full equality operator (=) is used, these two object variables must point to the same instance of a class (that is, the same object ).
20. objects and references
Php references an alias, that is, two different variable names point to the same content. In php5, an object variable no longer saves the value of the entire object.
Only saves an identifier to access the real object content. When an object is passed as a parameter and returned as a result, or assigned to another variable
The external variables are not referenced, but they all store copies of the same identifier. this identifier points to the true
Content

Author: JL_liuning

Bytes. Several companies can have basic PHP interview questions...

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.