Object oriented object-oriented foundation object-oriented what is a class?
A collection of a series of individuals with the same attributes (characteristics) and methods (behaviors), which is an abstract concept.
What is an object?
From a class, an individual with a specific property value, called an object. The object is a specific individual.
eg: human; Zhang San
Class and object relationships?
- A class is an abstraction of an Object! Object is the materialization of the class!
- The class simply indicates which properties of such an object, but cannot have a specific value, so the class is abstract.
- The object is to assign values to all the properties of a class, resulting in specific individuals, all of which are specific.
The declaration of a class and the instantiation of how to declare a class:
class 类名{ 访问修饰符 $属性[=默认值]; [访问修饰符]function 方法(){}}
class SimpleClass { // property declaration public$var'a default value'; // method declaration publicfunction displayVar() { echo$this->var; }}
Considerations for declaring a class:
- The class name can only have alphanumeric underline, the beginning cannot be the number, must conform to the big hump law;
- The class name must be
class
decorated, and must not have () after the class name;
- The property must have an access modifier, and the method can have no access modifier.
Instantiating objects and the invocation of object property methods:
$对象名 = new 类名(); //()可以不带
Call properties and methods outside the class:
$对象名 -> $属性名; //使用->调用属性时,属性名不能带$符号
Class calls properties and methods internally:
$this -> $属性名;
constructor what is a constructor function?
A constructor is a special function in a class that is equivalent to invoking the constructor of a class when we instantiate an object with the New keyword.
What is the function of the constructor?
When instantiating an object, it is automatically invoked to assign an initial value to an object's properties!
The constructor's notation:
Constructor name, must have the same name as the class (obsolete)
[public]function Person($name){$this$name;}
Using the Magic Method__construct
[public]function__construct($name){$this$name;}
Constructor considerations:
- The first one, the constructor name must be the same name as the Class!!!
- If a class does not have a handwriting constructor, the system will have an empty parameter construct by default, so it can be used
new Person()
;
- If we write a constructor with parameters, we will no longer have null parameter constructs, that is, it cannot be used directly
new Person()
;
Person
The ()
parameter list in the following must conform to the requirements of the constructor!!!!
- If two constructors exist at the same time, they will be used
__construct
.
Destructors:
__destruct()
:
- Destructors are automatically called before objects are destroyed;
- Destructors cannot have any parameters;
- Destructors are commonly used when objects are exhausted, resources are freed, resources are closed, and so on.
Magic Method:
PHP, give us a series __
of functions with the beginning, these functions do not need to call manually,
are called automatically at the right time, and such functions are called magic functions.
Eg: function __construct(){}
automatically called when class new is an object
function __destruct(){}
Called automatically when an object is destroyed, we ask that, in addition to the Magic method, custom functions and methods cannot be used at the __
beginning. Finally, for classes that have more complex functions, we will write to a class file separately. The name of the class file, the same lowercase, is "类名小写.class.php"
named in the way it is used. When you use this class in other files, you can use include to import the ".class.php"
file.
Encapsulation and inheritance what is encapsulation?
By accessing modifiers, properties and methods in the class that do not require external access are privatized to implement access control.
Note: Access control is implemented, not access denied. That is, after we privatize the attribute, we need to provide a corresponding method to let the user handle the property through the methods we provide.
The role of encapsulation?
- The user only cares about the functionality that the class can provide, and does not care about the details of the feature implementation! (Encapsulation method)
- Control the user's data, prevent the setting of illegal data, control the data returned to the user (attribute encapsulation +
set/get
method)
Implement encapsulation operations? Encapsulation of methods
For some methods that are only used internally within the class, rather than being used externally, we can use private to privatize the process.
privatefunction formatName()//这个方法仅仅能在类内部使用$this调用function showName(){ $this->formatName();}
Encapsulation of attributes +
set/get
Method
To control the setting and reading of properties, you can privatize the properties and ask the user to set them up by the method we provide set/get
private$age;//set方法function setAge($age){ $this->age=$age;//get方法function getAge(){ return$this->age;}$对象->getAge();$对象->setAge(12);
Encapsulation of attributes + Magic method
__get(__set(, ->= }$对象->age;//访问对象私有属性时,自动调用__get()魔术方法,并且将访问的属性名传给__get()方法;$对象->age=12;//设置对象私有属性时,自动调用__set()魔术方法,并且将设置的属性名以及属性值传给__set()方法;
Note: In the Magic method, you can use the branching structure to determine the difference between $key and different operations.
About the Magic method of encapsulation:
__set($key,$value)
: Automatically called when assigning a value to a class private property, passing two arguments to the method when called: Property name, property value to set.
__get($key,$value)
: Automatically called when the private property of a class is read, and a parameter is passed to the method when called, and the property name needs to be read;
__isset($key)
: isset()
automatically called when external functions are used to detect private properties.
The Isset () is used outside the class, and the private property is detected, which is not detected by default. False
Therefore, we can use the __isset () function, which returns the internal detection results when automatically called.
function __isset($key){return isset($this -> $key);}
When externally using the Isset ($ object name-and private property), the results returned by the above __isset () are automatically invoked when detected!
__unset($key)
: The external use unset()
of the function to delete the private property, automatically called;
function __unset($key){unset($this -> $key);}
When external uses unset ($ object name-and private property), the property name is automatically passed to __unset () when the property is deleted, and is handled by this magic method.
Inheritance basics: How to implement inheritance?
Use keywords for subclasses extends
, let subclass inherit parent class;
classextends Person{}
What are the considerations that are now inherited?
- Subclasses can only inherit non-private properties of the parent class.
- After the subclass inherits the parent class, it is equivalent to copy the parent class's properties and methods to the subclass, which can be called directly using the $this.
PHP can only be inherited, and one class is not supported to inherit multiple classes. However, a class inherits multiple layers.
class Person{}classextends Person{}classextends//Student 类就同时具有了Adult类和Person类的属性和方法
Method overrides (method overrides)
- Subclass Inherits Parent Class
Subclasses overriding parent classes already have methods
Meet the above two conditions, called method overrides. After overwriting, the subclass calls the method, and the subclass's own method is called.
Similarly, in addition to method overrides, subclasses can have properties with the same name as the parent class, overriding the property.
If the subclass overrides the parent class method, how do I call the parent class method in the subclass?
partent::方法名();
Therefore, when the subclass inherits the parent class, the first step in the construction of the subclass is to call the parent class construct to replicate.
function__construct($name,$sex,$school){ partent::__construct($name,$sex); $this$school;}
The PHP keyword final
final
Decorated class, this class is the final class and cannot be inherited!
final
Modification method, this method is the final method, cannot be overridden!
final
Properties cannot be decorated.
Static
- Properties and methods can be modified, respectively called static properties and static methods, also called Class properties, class methods;
Static properties, static methods, can only be called directly using the class name.
Use "Class name::\ (static property", "Class Name:: Static method ()" ' person::\)sex; Person::say (); '
- Static properties and methods are declared when the class is loaded, before the object is generated.
In a static method, a non-static property or method cannot be called;
Non-static methods, you can call static properties and methods. (Because static properties and methods have been generated when a class is loaded, not a static property method, at which point there is no instantiation of the birth)
In a class, you can use a self
keyword to refer to this class name.
class Person{static$sex"nan";function say(){ echoself::$sex;}}
Static properties are shared, that is, new has many objects and is a common attribute.
The CONST keyword:
Declare a constant in a class, not a define()
function! Keywords must be used const
. define()
similar to declarations, const
keyword declaration constants cannot be taken $
and must all be capitalized!
Once a constant is declared, it cannot be changed. Called at the static
same time as the class name Person::常量
.
Instanceof Operator:
Detects whether an object is an instance of a class. (including fathers, grandparents, grandparents ...) )
$zhangsaninstanceof Person;
"Small Summary" of several special operators:
.
can only connect strings;"".""
=>
When declaring an array, the associated key and value["key"=>"value"]
->
The object ( $this new
out of the object) invokes the member property, the member method;
::
Use the parent
keyword to invoke a method with the same name in the parent class: parent::say();
Use the class name (and self
) to invoke static properties, static methods, and constants in the class.
Magic Method Small Summary
-
__construct ()
: constructor, which is called automatically when an object is new.
-
__destruct ()
: destructors, which are called automatically before an object is destroyed.
-
__get ()
: called automatically when accessing private properties in a class. Pass Read property name, return $this, property name
-
__set ()
: automatically called when assigning a value to a class's private property. Pass property names and property values that need to be set;
-
__isset ()
: Automatically called when an object private property is detected using isset ()
. Pass the detected property name, return isset ($this, property name);
-
__unset ()
: Automatically called when an object private property is deleted using unset ()
. Pass the deleted property name, and execute unset ($this, property name) in the method;
-
__tostring (
): called automatically when you print an object using echo
. Returns the content that you want to display when you print an object, the return must be a string,
-
__call ()
: called automatically when a method that is undefined or not exposed in a class is invoked. Pass the called function name, and an array of argument lists;
-
__clone ()
: Automatically called when an object is cloned using the clone
keyword. The function is to initialize the assigned value for the newly cloned object;
-
__sleep ()
: called automatically when the object is serialized. Returns an array in which the values in the array are properties that can be serialized;
-
__wakeup ()
: Automatically called when an object is deserialized. Initializes the newly generated object for initialization assignment;
-
__autoload ()
: You need to declare a function outside of the class. Called automatically when an undeclared class is instantiated. Passing the instantiated class name, you can automatically load the corresponding class file using the class name.
Abstract classes and abstract methods
What is an abstract method?
Methods that do not have method bodies {}
must be decorated with a abstract
keyword. Such a method, which we call the abstract method.
abstractfunction say();//抽象方法
What is an abstract class?
abstract
classes that use keyword adornments are abstract classes.
abstractclass Person{}
Considerations for Abstract Classes:
- Abstract classes can contain non-abstract methods;
- Classes that contain abstract methods must be abstract classes, and abstract classes do not necessarily have to contain abstract methods;
- Abstract class, cannot be instantiated. (Abstract classes may contain abstract methods, abstract methods have no method body, instantiation calls have no meaning)
The purpose of our use of abstract classes is to restrict instantiation!!!
- Subclass inherits the abstract class, the subclass must override all the abstract methods of the parent class, unless the subclass is also an abstract class.
What is the use of abstract classes?
- Restrict instantiation. (abstract class is an incomplete class, the abstract method inside does not have a method body, so can not be instantiated)
- Abstract classes provide a specification for the inheritance of subclasses, which inherit an abstract class, and must contain and implement an abstract method that is already defined in the abstract class.
Interface vs. Polymorphic interface What is an interface?
An interface is a specification that provides a set of method combinations that must be implemented by a class that implements an interface.
The interface uses the interface
keyword declaration;
interface Inter{}
- All methods in an interface must be abstract methods.
- An abstract method in an interface does not require or use
abstract
adornments.
- The interface cannot declare variables, cannot have attributes, only use Constants!!!
Interface can inherit interface, use extends keyword!
The interface uses the extends inheritance interface to implement multiple inheritance.
interfaceextends Inter,Inter2{}
Class can implement an interface, using the
implements
Key Words!
Class uses implements
implementation interface, can implement multiple interfaces at the same time, multiple interfaces separated by commas;
abstract class Person implements Inter,Inter2{}
A class implements one or more interfaces, then this class must implement all the abstract methods in all interfaces!
Unless, this class is an abstract class.
Interface && Abstract class differences
- Declaratively, the interface uses the
interface
keyword, which is used by the abstract class abstract class
.
- Implementation/Inheritance method, a class uses the
extends
inherited abstract class, using the implements
implementation interface.
- Abstract classes can only be inherited, and interfaces can be implemented more than one. (Interface
extends
interface), multi-implementation (class implements
interface)
- Abstract classes can have non-abstract methods, interfaces can only have abstract methods, there is no cost abstract method. Abstract methods in abstract classes must be
abstract
decorated with keywords, and abstract methods in an interface cannot have modifiers.
- An abstract class is a class that can have properties, variables, and only constants in an interface.
Polymorphic polymorphic A class that is inherited by multiple subclasses.
If a method of this class, in multiple subclasses, shows different functions, we call this behavior polymorphic.
The necessary way to achieve polymorphism:
- Subclass inherits the parent class;
- Subclasses override the parent class method;
- Parent class reference to child class object
PHP Object-oriented