PHP object-oriented Summary

Source: Internet
Author: User

PHP Technology Exchange Group 170855791

Declaration class:
Class goodclass {} // The class name is usually written in camper mode.
Note: Because PHP internally defines the class stdclass, you cannotCodeThe class named stdclass
Class Attribute declaration must use a keyword indicating its visibility, such as public. The same is true for methods in the class. If this keyword is not found before the method, the default value is public.
In fact, PhP4 also supports var $ var; to declare attributes, and PhP5 also supports this method, but it is best not to write it like this.
If an attribute in the class is assigned an initial value during declaration, the value must be a constant and cannot be an expression.

  1   class   goodclass   2  {  3   Public   $ var1  = 123; ///   correct   4   Public   $ var2  = get_date (); ///   error   5   Public   $ var3  =  $ num  *  $ num ; ///   error   6 }

After defining the class, you need to create the class instance object and use the new keyword

 
1 $ GC=NewGoodclass ();

Delete object:

 
Unset($ GC);

Technically speaking, it is not necessary to delete objects becauseProgramAfter the execution is complete, the object is automatically deleted. But doing so can make the program look more neat

Note: in PHP, class names are case-insensitive, but object names and variable names are case-sensitive. The function names in PHP are case-insensitive, and the method names in the class are the same.

Use $ this to call another method or function inside the class, $ this refers to the current class (there are a lot of operation methods in Method management in variable and type-related extensions in the PHP Manual)

Deconstruct _ destruct cannot accept any parameters

Automatic loading class:
PhP5 supports a special function named _ autoload. when an object is created, if the class used is undefined, The __autoload method will be called.

1 Function_ Autoload ($ Class)2 {3Require_once($ Class. '. Php');4 }5 $ OBJ=New Class();

At this time, the class. php file will be imported
Another method: Spl_autoload_register ()

When creating an object, PHP always calls the constructor of the class to which the object belongs, including the constructor.

Method coverage:
The method defined by the subclass must have the same name and number of parameters as the method of the parent class.
Note: Method overloading can also be performed in PHP.

You can only access static attributes through self.
Only static member attributes can be accessed in static methods.
When a static method is accessed through class: function (), the class constructor is not called.
Static methods cannot be accessed through class instances.
Rewrite static methods. The rewrite method must also be static.

Exception:
There are two ways to throw an exception. The first is to explicitly throw an exception:
Throw new exception ('eror message ');
In this way, an exception type object will be thrown, and the code after throw will never be executed. This class is actually defined in PHP. To capture this field, catch (exception $ E) is required)
$ E is an exception-type object.
The second way to throw an exception is to use the try catch statement.
Methods In the exception class:

Name Return Value
Getcode () Code received
Getmessage () Received message
GetFile () Name of the file with an exception
Getline () Row number of the Code that throws an exception
Gettrace () Information Group, including file name and row number
Gettraceasstring () Similar to gettrace, but in the form of a string
_ Tostring () Convert all information into strings

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.