PHP Most full notes (four) (worth to collect, from time to glance)

Source: Internet
Author: User
Tags autoload closure object serialization setcookie

Serialization (serialized)

# data transfer is a string type

# In addition to resource types, all serializable

# serialization stores the data itself, as well as the data type when storing the data

1. When transmitting data on the network; 2. In order to place an array or an object on disk

# serialization Serialize produces a value that can be stored as a representation of a string serialize (mixed $value)

Returns a string that contains a byte stream representing value that can be stored anywhere. -facilitates the storage or delivery of PHP values without losing their type and structure. # deserialization Unserialize creates PHP values from stored representations mixed unserialize (string $str [, String $callback])-operates on a single serialized variable and converts it back to PHP's Value.

# file read and write operations

-File_put_contents writes a string to the file

int file_put_contents ($file, $data [, $flags])

$flags: File_use_include_path (overlay), File_append (append)-file_get_contents reads the entire file into a string file_get_contents ($file [, b Ool $use _include_path [, int $offset [, Int$maxlen]])

# object serialization

-only data inside the object can be serialized, that is, non-static properties.

# You need to load the class before deserializing the object, or you can trigger the auto-load mechanism.

__sleep serializes the attributes that need to be serialized.

-Commit uncommitted data, or similar cleanup operations, partially serialize the object.

-Returns an array containing all the variable names that should be serialized in the object

__wakeup the resources required for pre-preparing objects when deserializing

-Re-establish the database connection or perform other initialization operations

Public Function __sleep () {

Return array (' Server ', ' username ', ' password ', ' db ');

}

Public Function __wakeup () {

$this->connect ();

}

Object inheritance

Class subclass name extends parent class {}

If an object is an object of a subclass, it is also an object of the parent class.

Single inheritance: A class can inherit only one parent class and cannot inherit multiple classes at the same time. However, a parent class can be inherited by multiple subclasses.

Instanceof to determine whether an object is a class object

Object name instanceof class name

Access control

Public (inheritance chain, this class, externally accessible) protected protected (inheritance chain only, this class is accessible) private (only this class is accessible)

Judged by the member definition location and access location.

# compatibility issues

-When declaring a property, the default for the VAR keyword declaration is public permission-when declaring a method, omit the access modifier, default to public permission

overriding override

$this represents the object, which is called by whom. -When inheriting, the subclass member name conflicts with the parent class member name, and the child class member overrides the parent class member. -Properties and methods can be overridden by the quilt class. -Override is required when the parent class's method or property already does not meet the requirements of the subclass. -may also be overridden because of a naming nonstandard.

Private properties cannot be overridden, and each private property is logged. The class is also logged when the property name is recorded.

If a built-in function is overridden, the parent class method can be called. such as calling the parent class construction method Parent::__construct ()

# Override Restrictions

Access Restrictions:

The access control for a member of a subclass must be equal or weaker than the parent class.

Method parameter Restrictions:

The number of parameters must be the same, parameter names can be different.

# $this Determine the principle

$this is the object that invokes the method, representing the execution environment object of the method.

-Object Invocation

-The delivery of the environment. If the value of $this (static call) is not determined at the time of the current call, the object environment in which the static call is made is passed to the called method. $this does not always represent this object, but is determined by the execution environment of the method.

# Final If a method in the parent class is declared final, the child class cannot overwrite (override) the method.

If a class is declared final, it cannot be inherited.

But the class with the final keyword can still be instantiated!

# abstract class

Keywords: abstract

An abstract class cannot be instantiated directly, it must inherit the abstract class first, and then instantiate the subclass.

At least one abstract method should be included in the abstract class. Non-abstract classes cannot contain abstract methods.

If a class method is declared abstract, it cannot include a specific feature implementation. Abstract methods cannot contain curly braces and method bodies.

When inheriting an abstract class, the subclass must implement all the abstract methods in the abstract class.

That is, subclasses must override all abstract methods in the abstract parent class.

In addition, the visibility of these methods must be the same (or looser) as in the abstract class.

That is, if an abstract method in an abstract class is declared as protected, then the methods implemented in the subclass should be declared as protected or public, not private. -Ordinary methods in subclasses of abstract classes are executed in the same way as other classes. Role

1. Inheritance, for extended classes, unified public operations.

2. Restriction structure (specification). The structure of the canonical subclass.

Interface

Keyword: interface

-The way an object provides interaction with an object is an interface. -Use an interface to specify which methods a class must implement, but you do not need to define the specifics of these methods. -Define an interface by interface, just like defining a standard class, but all of the methods defined in it are empty. -All properties and methods defined in the interface must be public, omitting the public keyword. -The constant (const) can also be defined in the interface. Interface constants and class constants are used exactly the same.

Can be used:: Access. Interface Name:: Constant Name, implementation class:: Constant name.

They are fixed values that can be used by a quilt class or subinterface, but cannot be modified. -Interface cannot define attributes! # define interface Interface interface Name {

Interface content (a collection of public method declarations)

}

# interface Implementation

-To implement an interface, you can use the implements operator. -All methods defined in the interface must be implemented in the class, or 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, the methods in the interface cannot have duplicate names. -interfaces can also be inherited by using the extends operator. Class name implements interface name {

Implementation of interface method

}

# Note

1. A class is an inheritance relationship between classes and an abstract class, and a class is an implementation relationship with an interface.

2. Classes and abstract classes are single-inheritance, and classes and interfaces are multiple implementations.

3. The interface is not a class and restricts the structure of the class.

4. There is multiple inheritance between the interface and the interface. Use the extends keyword.

Interface I_c extends I_a, I_b {}

Static delay Binding

Self::, represents this class (the current code is in the same class)

Always represents this class because it has been determined at the time of class compilation.

That is, the child class calls the parent class method, and self does not represent the calling subclass. Static::, representing this class (the class that called the method)

The class used to reference static calls within the inheritance scope.

The class to represent is determined at run time.

Static:: Is no longer parsed to define the class in which the current method resides, but is calculated at the actual run time.

Traversal of an object (iteration)

-Objects hold data through attributes, so they traverse the properties of the object. -The Foreach language structure, which obtains the property name and the property value.

foreach ($obj as $p _name = $p _value) {}# custom traversal (iterator iterator)

Iterator-an interface that iterates its own external iterators or classes internally

iterator::current-returns the current element

iterator::key-returns the key of the current element

Iterator::next-move forward to the next element

Iterator::rewind-returns to the first element of an iterator

iterator::valid-Check if the current location is valid

# Cloning of objects

The transfer value between objects is a [reference] pass.

Clone: New object = Clone Old Object

-All reference properties will still be a reference to the original variable.

The __clone () method is called automatically when an object is cloned.

Note: The construction method corresponds to the instantiation (new), and the Clone method corresponds to clone.

Single-Case mode

#三私一公单例模式 (Singleton) is used to generate a unique object for a class. The most common place is the database connection. Once an object is generated using singleton mode, the object can be used by many other objects. # prevents a class from being instantiated multiple times class MySQLdb {

private static $instance = null; Save class instance in this property

The construction method is declared private, preventing the object from being created directly

Private Function __construct () {}

public static function getinstance () {

if (! Self:: $instance instanceof Static) {

Self:: $instance = new static;

}

Return self:: $instance;

}

Private Function __clone () {}//block user from copying object instance}

Magic method

__construct Construction Method

__destruct destructor method

__clone Cloning objects

__sleep Serialized Objects

__wakeup deserializing objects

__autoload automatically loaded, when using classes but not found

When the __tostring object is used as a string

__invoke when you try to invoke an object in a way that invokes a function

# Heavy-duty overload

Dynamic "Create" class properties and methods

The user is free to add additional attributes to the object, which is overloaded.

All overloaded methods must be declared public.

Overloaded methods are called when a class property or method is called that is undefined or not visible under the current environment.

Parameters for overloaded related magic methods cannot be passed by reference.

# Property Overloading

-Handle Inaccessible properties

Property overloading can only be done in an object.

# property overloads are not valid for static properties

In static methods, these magic methods will not be called. So none of these methods can be declared static.

__set when assigning a value to an inaccessible property

public void __set (string $name, mixed $value)

Role: Bulk management of private properties, indirect protection of object structure

__get reading the value of an inaccessible property

Public mixed __get (string $name)

__isset when Isset () or empty () is called on an inaccessible property

public bool __isset (string $name)

__unset when unset () is called on a property that is not accessible

public void __unset (string $name)

# method overloading

-handling of inaccessible methods

__call is called automatically when an inaccessible non-static method is invoked, such as undefined or invisible

Public mixed __call (string $name, array $arguments)

__callstatic automatically called when an inaccessible static method, such as undefined or invisible, is invoked

public static mixed __callstatic (string $name, array $arguments)

# $name parameter is the name of the method to invoke. The $arguments parameter is an array that contains the arguments to pass to the method.

Type constraints

The parameters of the function can be specified only for objects or arrays

The object is qualified to add the class name before the formal parameter, and an array is specified before the formal parameter.

Type constraints allow null values

Type constraints are not only used in the member methods of a class, but also in functions.

Three major features

Encapsulation: Hidden interior is absorbed, only development interface.

Inheritance: The members of one object are used by another object. The syntax is embodied in the common use of code.

Polymorphism: Multiple forms.

Classes and Objects ·

The keyword this represents public ownership of this object (inheritance chain, this class, externally accessible) protected protected (inheritance chain only, this class is accessible) private (only this class is accessible)

Parent:: Represents the Parental class

Self:: Represents this class (the current code is in the same class) Static:: Represents this class (the class that called the method), Statics (properties, methods), all objects can be used, external can be directly used or modified, static methods cannot access non-static members final Method with final non-quilt overload, class with final cannot be inherited (method, Class) Const class constant (property) abstract class interface interface extends class inheritance (sub-Interface inheritance interface, other ordinary class inheritance) Implements interface implementation (class implementation interface, abstract class implementation excuse) (the implementation of the interface and inheritance can have multiple)

Iterator built-in interface (iterative) clone cloning

Instance instances

instanceof whether an object belongs to a class

/* Class-Object-related functions */

Class_alias ([$original [, $alias]]) gives the class an alias class_exists ($class [, $autoload]) to check whether the class is defined interface_exists ($interface [, $ AutoLoad]) Check if the interface has been defined method_exists ($obj, $method) Check if the method of the class exists

Property_exists ($class, $property) checks whether an object or class has this property get_declared_classes (void) returns an array of the names of the defined classes Get_declared_ Interfaces (void) returns an array containing all declared interfaces Get_class ([$obj]) The class name of the returned object Get_parent_class ([$obj]) returns the parent class name of the object or class Get_class_methods ( $class) returns an array of the method names of the class Get_object_vars ($obj) returns an associative array of object Properties Get_class_vars ($class) returns an array of the default properties of the class Is_a ($obj, $class) Returns TRUEIS_SUBCLASS_OF ($obj, $class) if the object belongs to the class or if the class is the parent of this object, returns Trueget_object_vars ($obj) returns an associative array of object properties if this object is a subclass of that class

Common classes

# PHP Manual, predefined class closure closure class, final class of anonymous function object

StdClass standard class, typically used for object classes to hold collection data

__php_incomplete_class an incomplete class, when only the object is not found, the object is considered to be the object of the class exception exception class

PDO data Object class

Magic Constants

The full path (absolute path) and file name of the current line number __file__ file in the directory __line__ file where the __dir__ file resides

The name of the __class__ class is the method name of the __method__ class, containing the class name and method name __function__ function name, which only represents the method name within the method

Reflection Mechanism Reflection

Effect: 1. Gets the structure information 2. Agent execution

Reflectionclass report information about a class

Reflectionmethod report information about a method

Reflectionclass::export Output Class Structure Report # Agent performs instantiation of objects of the Reflectionfunction class

$f = new Reflectionfunction (' func '); Func is function func ($p)

$f->invoke (' param ');

/* Page Jump */

Phpheader (' Loacation:url ') header () after execution, the subsequent code will continue to execute, so you need to add die after the statement end

Unable to give prompt, direct jump//JS method

Location.href = url//HTML

<meta http-equiv= "Refresh" content= "indicates the value of the time; Url= the URI to jump ">

/* "Cookie" */

A cookie is a mechanism for storing data on a remote browser to track and identify users.

A cookie is part of the HTTP header, so the Setcookie () function must be called before other information is exported to the browser, similar to the limit on the header () function. You can use the output buffering function to delay the output of the script until all cookies or other HTTP headers are set as needed.

New

Setcookie added a cookie message Setcookie ($name [, $value [, $expire [, $path [, $domain [, $secure [, $httponly]] []]] #注意: Setcookie ( ) cannot have output before the function! Unless the OB cache is turned on!

# parameter Description $name-cookie's distinguished name

Use $_cookie[' name '] to redeem a Cookie$value-cookie value named Name, which can be a numeric value or a string that is saved on the client and not used to hold sensitive data

Assuming that the value of the $name parameter is ' name ', then $_cookie[' name ' will get the lifetime of the $value value $expire-cookie (Unix timestamp, number of seconds)

If the value of the $expire parameter is time () +60*60*24*7 you can set the cookie to expire after one week. If this parameter is not set, the session is invalidated immediately. $path-cookie The specified path on the server side. When this value is set, only the Web page or program under the specified path in the server can access the cookie.

If the parameter value is '/', the cookie is valid throughout the domain.

If set to '/foo/', the cookie is valid within the/foo/directory and its subdirectories under domain.

The default value is the current directory where the cookie is set and its subdirectories. $domain-Specifies the URL name of the server to which this cookie belongs, and the default is to establish the URL of this cookie server.

The entry should be set to '. ABC.com ' If the cookie is valid for all subdomains under the abc.com domain name. $secure-Indicates whether the cookie only passes the security identification constant of the cookie through a secure HTTPS connection, and if this value is set, it means that only in some cases can it be passed between the client and the server.

When set to true, the cookie is only set in a secure connection. The default value is False.

Read

-All cookie information under the current domain name is brought to the server when the browser requests it. -any cookie sent from the client will be automatically stored in the $_cookie global array. -If you want to set multiple values for a cookie variable, add the [] symbol to the name of the cookie. That is, storing multiple data in array form to the same variable.

Set to $_cookie[' user ' [' name '], note that the name of User[name] does not have quotation marks

Setcookie (' user[name] ', ' shocker ');-$_cookie can also be an indexed array

Remove Method 1: Set its value to an empty string

Setcookie (' user[name] ', ');

Method 2: Set the target cookie to the "expired" state.

The lifetime of the cookie is set to expire, and the lifetime is the same as the browser, which is deleted when the browser is closed.

Setcookie (' usr[name] ', ', Time ()-1);

Note

1. Cookies can only hold string data 2. $_cookie is used only to receive cookie data and not to set up or manage cookie data.

Manipulation of $_cookie does not affect cookie data.

$_cookie will only save the cookie data that the browser carries when requested. 3. Cookie life cycle:

Temporary cookie: Deleted when browser is closed

Persistent Cookie: $expire parameter is a timestamp that indicates the time of failure. 4. Valid directory

The cookie is valid only in the specified directory. The default is the current directory and its subdirectories.

The cookie in the subdirectory is not available in its parent directory or sibling directory. 5. cookie-Differentiated domain name

The default is the current domain name and its sub-domains are valid. 6. JS is obtained by Document.cookie, the type is String 7. The browser has no limit on the total number of cookies, but the number of cookies per domain name and the size of each cookie are limited and different browser restrictions apply.

PHP Most full notes (four) (worth to collect, from time to glance)

Related Article

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.