Home >Backend Development >PHP Tutorial >Summary of PHP object-oriented basic knowledge_PHP tutorial
I recently participated in several interviews with PHP engineers, but my answers to the written test questions were not satisfactory. When I came back, I summarized the reason for the failure. It was because I did not read the PHP manual. PHP basic interview questions from several companies can be found in the PHP manual. Hey, now I know that the best interview guide is the PHP manual.
The following are excerpts from some PHP object-oriented basics, taken from the PHP5.1 manual.
1. The variable members of a class are called "properties", or "fields" or "features", and are collectively referred to as "properties" in this document.
2. The variables in the attributes can be initialized, but the initialized value must be a constant. The constant here means that the php script is a constant during the compilation stage, not
Constants that are evaluated during the runtime phase after the compile phase.
3. In the member method of the class, you can access the properties and methods of the class through $this->property (property is the property name), but
To access static properties of a class or within a static method, use self::$property instead.
4. You can use the pseudo variable $this in the non-static method of the class. This pseudo variable is the reference to the instantiated object that calls the method
5. The value of a constant must be a fixed value, no modification is allowed, and it cannot be the result of a variable, class attribute or other operation (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 will call this method first every time it creates an object, so it is very suitable to do some initialization work before using the object.
If a constructor is defined in a subclass, the constructor of its parent class will not be implicitly called. To execute the constructor of the parent class, you need to
in the constructor of the subclass
Call parent::__construct().
7. The destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.
The destructor of the parent class is not implicitly called by the engine. To execute the destructor of the parent class, you must explicitly call
in the destructor body of the child class
parent::__destruct().
The destructor is called when the script is closed, after all header information has been emitted.
Attempting to throw an exception in the destructor results in a fatal error.
8. When extending a class, the subclass will inherit all the public and protected methods of the parent class. But the methods of the subclass will override the methods of the parent class.
9. Range resolution operator (::), which can be used to access static members, methods and constants
When accessing these static members, methods, and constants from outside the class, the class name must be used.
The two special keywords self and parent are used to access members or methods inside the class.
10. When a subclass overrides a method in its parent class, PHP will no longer execute the overridden methods in the parent class until these methods are called in the subclass. This
This mechanism also works with constructors and destructors, overloaded and magic functions.
11. Static variables and methods
By declaring a class member or method as static, you can access it directly without instantiating the class. Static members cannot be accessed through an object (static methods
except).
Since static methods do not require an object to be called, the pseudo variable $this is not available in static methods.
Static properties cannot be accessed by objects through the -> operator.
Calling a non-static method using the :: method will result in an E_STRICT level error.
Like all other PHP static variables, static properties can only be initialized to a character value or a constant, not expressions. So you can
You can initialize a static property to an integer or array, but it cannot point to another variable or function return value, nor to an object.
12. If "visibility" is not specified, properties and methods default to public.
13.Abstract class
Abstract classes cannot be instantiated directly; you must first inherit from the abstract class and then instantiate the subclass. An abstract class must contain at least one abstract method. if
Class methods are declared abstract, so they cannot include concrete functional 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 in the abstract class (
or looser). If an abstract method in an abstract class is declared protected, then the method implemented in the subclass should be declared protected
Or public, but cannot be defined as private.
Application example:
abstract class AbstractClass
{
// Force subclasses to define these methods
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Ordinary methods (non-abstract methods)
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 an interface, you can specify which methods a class must implement, but you do not need to define the specific content of these methods.
We can define an interface through interface, just like defining a standard class, but all methods defined in it are empty.
All methods defined in an interface must be public, which is a characteristic of the interface.
To implement an interface, use the implements operator. The class must implement all methods defined in the interface, otherwise a fatal error will be reported.
If you want to implement multiple interfaces, you can use commas to separate the names of multiple interfaces.
When implementing multiple interfaces, methods in the interfaces cannot have the same name.
Interfaces can also be inherited, using the extends operator.
Constants can also be defined in interfaces. Interface constants and class constants are used exactly the same. They are all fixed values and cannot be modified by subclasses or subinterfaces.
Application example:
//Interface definition
interface iTemplate
{
Public function setVariable($name, $var);
Public function getHtml($template);
}
//Use 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 a function to iterate objects. Just like using an array, you can traverse the properties in the object through foreach.
By default, external iteration can only get the values of externally visible properties.
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 =>
}
}
}
$class = new MyClass();
foreach($class as $key => $value) {
Print "$key => $valuen";
}
echo "n";
$class->iterateVisible();
?>
16. Design Patterns
The Factory pattern allows you to instantiate objects while your code is executing. It is called the factory pattern because it is responsible for "producing" objects. Factory
The parameter of the method is the class name corresponding to the object you want to generate.
Singleton pattern is used to generate a unique object for a class. The most commonly used place is database connection. Use singleton pattern to generate a
After an object is created, the object can be used by many other objects.
Application example:
class Example
{
//Save the class instance in this attribute
Private static $instance;
// The constructor is declared as private to prevent direct creation of objects
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;
}
// Ordinary methods in Example class
Public function bark()
{
echo 'Woof!';
}
// Prevent 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 object of Example class.
// This way of writing will be wrong because the constructor is declared as private
$test = new Example;
//The following will get the singleton object of the Example class
$test = Example::singleton();
$test->bark();
// Copying the object will result in an E_USER_ERROR.
$test_clone = clone $test;
?>
17.PHP 5 adds a new final keyword. If a method in the parent class is declared final, the subclass cannot override the method; if a class is declared final
If declared final, it cannot be inherited.
18. Object copying can be completed through the clone keyword (if the __clone() method exists in the object, it will be called first). __clone()
in object
Methods cannot be called directly.
$copy_of_object = clone $object;
When an object is copied, PHP5 will perform a "shallow copy" of all properties of the object. All references in attributes are still not
Change, pointing to the original variable. If the __clone() method is defined, the __clone() method in the newly created object (the object generated by copying) will be called
can be used to modify the value of a property (if necessary).
19. Object comparison
When using the comparison operator (==) to compare two object variables, the comparison principle is: if the attributes and attribute values of the two objects are equal, and the two objects
are instances of the same class, then the two object variables are equal. www.2cto.com
And if you use the equality operator (===), the two object variables must point to the same instance of a certain class (that is, the same object).
20. Objects and references
PHP references are aliases, that is, two different variable names point to the same content. In PHP5, an object variable no longer holds the value of the entire object.
Just save an identifier to access the real object content. When an object is passed as a parameter, returned as a result, or assigned to another variable, another
The outer variable has no reference relationship with the original one, but they both store copies of the same identifier, which points to the real
of the same object.
Content
Author: JL_liuning