PHPClass class and object learning NOTE _ PHP Tutorial

Source: Internet
Author: User
PHPClass class and object learning notes. The PHPClass class and object learning notes written in the course are as follows. I hope this tutorial will help you with your learning classes and objects. Personal notes ..... I will share with you the following PHP Class and object learning notes I have written in my study. I hope this tutorial will help you with your learning classes and objects.

Personal notes... for reference. if you have any mistakes, please point out. thank you.

The code is as follows:

/*************************************** ****

Class object instance description (common Class)

**************************************** ***/
Class ClassDemo {
Public $ PublicVar;

Private $ PrivateVar; // private variables cannot be called outside

Protected $ ProtectedVar; // The externally protected variable is not accessible to the subclass.

Public static $ StaticVar = 0; // static variables static methods cannot access non-static methods and variables, non-static methods and variables can access static methods and variables

Const constVar = ''; // when you have an attribute that you do not want to modify, consider using const as a constant, using class name: constant name; interface name: constant
/*
1. an initial value must be assigned to a constant during definition.
2. constants cannot be modified.
3. the constant name cannot have $, which is generally in upper case. the default value is public.
4. constants use the self: constant name inside the class. class name: constant name
*/
Public function _ construct (/* $ name */) {// this can contain a parameter. it must also contain a parameter when a parameter is created. Otherwise, an error may occur.
// $ This-> PublicVar = $ name;
// Self: $ StaticVar ++; // static variable internal access method external access method object name: variable name (ClassDemo: $ StaticVar)
$ This-> PublicVar = '$ PublicVar ';
$ This-> PrivateVar = '$ PrivateVar ';
$ This-> ProtectedVar = '$ ProtectedVar ';
Self: $ StaticVar ++;
Echo "ClassDemo _ construct
";
}

Public function _ destruct () {// destructor
}
Final public function fun (){
/*
Final keyword (in PHP5)
If you do not want the method to be overwritten by the inheritance time of other classes, you can use final

When this keyword is used to modify a class, the class cannot be inherited by other classes (can be instantiated)

Note: This keyword cannot be used to modify variables.

*/
}
} // End Class
/*************************************** ****

Class object instance description (abstract Class)

**************************************** ***/
Abstract class AbstractDemo {
/***************************************

1. abstract classes cannot be instantiated.

2. abstract classes do not have to include abstract methods.
That is to say, the abstract class can have no abstract method.

3. Once the abstract method is included, this class must
Declare as abstract

4. abstract classes cannot have function bodies

5. if a class continues an abstract class, it must implement
All abstract methods of this abstract class. (Unless it is declared as an abstract class)

***************************************/
}

/*************************************** ****

Class object instance description (inherited Class)

**************************************** ***/
Class DemoTwo extends ClassDemo {

}
/*************************************** ****

Class object instance description (interface)

**************************************** ***/
Interface Face {
/*********************************
1. when a class implements an interface, it is required that the class must implement all methods of this interface.
2. methods of the interface cannot have a method body.
3. an interface cannot be instantiated.
4. the interface can have attributes, but must be a constant and public

When to use the interface

1. set specifications
2. set standards for other programmers to implement, such:

**********************************/
Public function Name ();

}

Interface Face2 extends Face {
/*******************************

Inheritance interface
The method for implementing the parent interface when inheriting interfaces

********************************/

Const namevar = 20;

}

Class Demo implements Face2 {
/**********************************

Implementation interface. multiple interfaces can be implemented at the same time.
When a class implements some interfaces, all interfaces and methods must be implemented.

**********************************/

Public $ Name1 = 0;

Public function Name (){

Echo Face2: namevar;

}

}

?>

Summary

Use self: (note not $ slef) to access static member attributes or methods within the class, for example:

The code is as follows:

Slef: $ country
Slef: myCountry ()

When the subclass accesses the static member attributes or methods of the parent class, use parent: :( Note not $ parent), for example:

The code is as follows:

Parent: $ country
Parent: myCountry ()

For external access, the static member attributes and methods are class name/subclass name:, for example:

The code is as follows:

Person: $ country
Person: myCountry ()
Student: $ country

However, static methods can also be accessed through common objects.


• Class/object functions

• _ Autoload-try to load undefined classes
• Call_user_method_array-call a user method and pass the parameter array (obsolete)
• Call_user_method-call the user method for a specific object (obsolete)
• Class_alias-create an alias for a class
• Class_exists-check whether the class is defined
• Get_called_class-name of the Static Binding ("Late Static Binding") class
• Get_class_methods-returns an array composed of class method names
• Get_class_vars-returns an array composed of the default attributes of the class.
• Get_class-class name of the returned object
• Get_declared_classes-returns an array composed of the names of defined classes.
• Get_declared_interfaces-returns an array containing all declared interfaces.
• Get_declared_traits-returns an array of all defined traits
• Get_object_vars-returns an associated array composed of object attributes.
• Get_parent_class-return the parent class name of the object or class
• Interface_exists-check whether the interface has been defined
• Is_a-if the object belongs to this class or the class is the parent class of this object, TRUE is returned.
• Is_subclass_of-if this object is a subclass of this class, TRUE is returned.
• Method_exists-check whether the class method exists
• Property_exists-check whether the object or class has this attribute
• Trait_exists-check whether the specified trait exists

Examples Class and object learning notes. I hope this tutorial will be helpful to you. Personal notes... used for borrowing...

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.