PHP object-oriented programming (oop) learning notes (4)-Exception handling class Exception. Exception handling modules similar to other languages are added for the use of PHP5. Exceptions in PHP code can be thrown by throw statements and captured by catch statements. Usage exceptions that require exception handling
PHP5 adds exception handling modules similar t
obey, and after subclasses inherit abstract classes, the abstract methods inside the abstract class are implemented according to the needs of the subclasses. subclasses must implement all the abstract methods in the parent class, or else there are abstract methods in the subclass, then the subclass is abstract or the class cannot be instantiated ; Why do we have to inherit from the abstract class? Because sometimes we have to implement some functions must inherit from the abstract class, otherw
is conceived out, so oriented to abstract design is in line with human thinking. In addition, this principle will be well supported by the OCP, and the abstraction-oriented design allows us to not rely too much on implementation, so that the extension becomes possible, and this principle is the cornerstone of another article, design by contract.4) Interface segregation Principle (ISP), interface isolation principle, "breaking large interfaces into multiple small interfaces", the benefits are ob
we said earlier, PHP is single-inheritance, a class can have only one parent class, but a class may implement more than one interface, it is equivalent to a class to comply with a number of specifications, just as we have to abide by the laws of the state, if it is in school, but also abide by the school rules;
Implementing multiple interfaces with implements
Class four Implemtns interface one, interface two, ...
{
You must implement the methods in
PHP are the object name, the return value is a string, the string returned by Serialize () is ambiguous, and generally we do not parse the string to get the information of the object, we just need to upload the returned string to the other end of the network or save it to a file.The Unserialize () function in PHP crossdress the line object, which is the return value of the Serialize () function, and the ou
One , in OOP, there is a pattern called a singleton pattern, that What is a singleton mode? PHP Singleton mode is that an object is generated only once, but the object can be used by many other objects. In its core structure, it contains only a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be a
concrete see:
It can be seen from the $p1=new person (); The right side of the equals sign is a real object instance, in the heap memory of the entity, a total of 3 times the new person (), so will be in the heap open 3 space, produce 3 instance objects, each object is independent of each other, using their own space, In PHP, as soon as a new keyword appears, an object is instantiated, creating a space within the heap.
Each instance object in the he
usage of PHP classToday we have a basic usage of PHP class to tell you, first of all you understand what is a class: the definition of a class is as follows:
With uniqueness, flexibility, versatility, importance, which is defined by, and more detailed to the www.111cn.cn view is, well, in order to let everyone know more clearly what is oop we will come to the ex
{static $sex = "male";function __construct ($name, $sex) {$this->name= $name;Self:: $sex = $sex;}}echo Person:: $sex;⑥ static properties are shared. That is, new many objects are also shared with one property;3, the const keyword;* Declare constants in the class, you cannot use the Define () function! You must use the Const keyword.* Similar to the Define () declaration, the Const keyword declaration cannot take $ and must all be capitalized.* Constants once declared, cannot be changed. Called w
At the very beginning, when we use one or more classes, it is generally necessary to use the require or include function to introduce the class before it can be used;
But when there are so many classes in a project, we have to introduce them all in one file when we use them, so
On the one hand is more cumbersome, not high efficiency. On the other hand, when we delete some of these classes, we are going to delete the corresponding require or include
statement, error prone;
To avoid the abov
to $this, except that self is the class that represents this static method . In a static method, you can use the class name of the class in which the method resides, or you canuse "self" to access other static members, and if there is no special case, we usually use the latter, the "Self:: member Property ".It is also possible to access static members in non-static methods, but you cannot use the "$this" reference, or use the class name or "self: the form of a member property."Const is a keywor
a method body, which is similar to an abstract class. The difference between them is that an interface cannot declare any method that has a method body, and they use a different syntax. To force the uncover rule to be added to a class, we need to use the Implements keyword instead of the extends keyword.In some cases we need to determine whether a class is a type of a particular class, or whether a particular interface is implemented. Instanceof is divided into fit to complete this task. Instan
");} }As in the above code, only "name" is used when serializing an object with serialize (), and the "age" property is serialized.11.__wakeup (): Automatic invocation of object deserialization, initialization assignment for deserialization of newly generated objectsclass person{ public$name; Public $age ; function __wakeup () { $this , name = "Lisi"; }}Serializes the object and then deserializes it, assigning an initial value of "Lisi" to the object's
() function determines whether the interface exists and returns true if present, otherwise false.echo interface_exists (' computer ');The 7.is_a () function returns ture if the object is a class or is a parent class of this class, otherwise returns FALSE.Echo is_a ($computer, ' computer ');The 8.is_subclass_of () function determines whether the object is a subclass of the class, returns Ture, or returns false.Echo is_subclass_of ($notecomputer, ' computer ');The 9.method_exists () function dete
This article mainly introduces the method of magic in object-oriented oop in PHP, which has certain reference value, and the interested partners can refer to
One, what is the Magic method:
PHP provides us with a series of functions with the beginning, which do not need to be called manually, and will be called automatically at the appropriate time, such function
also possible to access static members in non-static methods, but you cannot use the "$this" reference, or use the class name or "self: the form of a member property."Const is a keyword that defines constants, and in PHP defines constants using the "define ()" function, but defining constants in a class uses the keyword "const", similar to the # in C Define if the value is changed in the program, an error occurs, and the member property that is decor
-existent method is passed to the first parameter, the second parameter is to take the method of the multiple parameters to The form of an array is passed in .PHP//This is a test class with no properties or methods.classtest{//methods that are called automatically when an existing method is called, the first parameter is the method name, the second argument is the array parameter function__call ($function _name,$args) { Print"The function
As we said before, the method that declares the method name of "__" in the class (provided to us by PHP) is the method that executes automatically at a certain time under different circumstances, and the "__tostring ()" method is also automatically called, which is called automatically when the object reference is directly exported. We said before that the object reference is a pointer, for example: "$p =new person ()", $p is a reference, we can not u
The Oop of PHP notes
├ the properties and methods of object access itself are accessed with "--", and no "$" is required to access the property;
├ class inside Use this keyword when you add "$" before this $this->name = "Firstep";
├ class constructor __construct () destructor __destruct ()
├ access modifier public protected (this class, subclass, parent class accessible) private
├ Package correlation func
the above code, only "name" is used when serializing an object with serialize (), and the "age" property is serialized.11.__wakeup (): Automatic invocation of object deserialization, initialization assignment for deserialization of newly generated objectsClass person{public $name; public $age; function __wakeup () { $this, name = "Lisi"; }}Serializes the object and then deserializes it, assigning an initial value of "Lisi" to the object's Name property.12.__
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.