How to define a class and its member properties in PHP

Source: Internet
Author: User
Class Concept: A class is a collection of objects that have the same properties and operations. It provides a uniform abstract description of all objects that belong to the class, including properties and operations in two main parts. In an object-oriented programming language, a class is a stand-alone program unit that should have a class name and consist of two main parts: a description of the property and a description of the operation.

1, the definition of the class:

I. Class keyword definition using class

1. Define an empty class

Class person{};

2. Define a class that has member properties and operations

Class person{member Properties ... To make a work of ...    }

3. Define a class that cannot be inherited, using the final keyword

Final class person{member Properties ... To make a work of ...    }

4. Note: The final keyword cannot be used to decorate member properties, only classes and methods can be decorated (described later in the final method)

5. Here is a final class

Defines a final class finalclass that contains a common function

Final class Finalclass {public Function ffun () {echo "This class is final";}}

Define a class Childfinalclass and inherit the Finalclass class

Class Childfinalclass extends Finalclass {public Function fchildfun () {echo ' This class inherits the final class Finalclass ';}}

This will prompt the system when executing the above command

Fatal Error:class Childfinalclass may not inherit from final Class (Finalclass)

Proof that class defined by the final keyword cannot be inherited by a quilt class

2. Definition of member properties in a class

I. member properties are some of the variable properties defined for the class, and that person, as a class, has a pair of eyes (normally, the exception of the two Lang gods), a mouth, two ears, and so on. Some of the proper nouns used to describe or represent something are called member properties.

Ii. keywords used by declarations of member properties in a class

Iii. Common member attribute declarations are made by closing the following key words

Public,var,protected,private begins with a variable to make it. There are also some member properties, static statics, and const constants.

Public: Represents a global, class-internal outer subclass that can be accessed

var:php in version 5, this member property is considered to be a public type property

Protected represents protected only in this class or subclass or in a parent class that can access

private means that only this class can be used internally.

static:1) static Properties,

2) member properties modified with static can be shared by all objects of the same class

3) static data is present in the in-memory data segment when the class is loaded for the first time (initializing static segments)

4) Use Self:: member property name in class

5) Outside class Name:: member Property name

CONST:1) class, declaring constants in a class must use the const

2) Use Self:: member property name in class

3) out-of-Class use class Name:: member Property name

Note: Variables in the attribute can be initialized, but the initialized value must be constant, where the constant is the PHP script as a constant at compile time, rather than a constant that is run at runtime after the compile phase. For example, you cannot include any operator, cannot be any variable, cannot be a function, and so on.

Iv. How member properties are invoked:

In the member method of the class, you can access the properties and methods of the class by $this->property (property is the attribute name), but to access the static properties of the class or to use it in a static method, use Self:: $property. A pseudo-variable $this can be used inside a non-static method of a class, which is an instantiated object reference that invokes the method.

Then use the code to explain the above content:

Class Finalclass {//static $a = strtolower ();   This syntax error//const  A = 1+2;              This error//public $name = 123+456;     This error is static $a = ' $a '; const  a = ' a ';p ublic $name = ' chicken sister ';p rotected $sex = ' male + female ';p rivate $age = 23;} Class Childfinalclass extends Finalclass{public function fchildfun () {echo ' Childfinalclass classes Childfinalclass:: $a- '. Childfinalclass:: $a. " 

3. Definition of operations in a class

I I generally like to call the operation as a member method, the following I will call the operation as the method is actually the same

Ii. definition of Member methods: Member methods are some of the functional methods defined for this class, such as example of taking this class, people can eat, can run, can knock code, this is the member method. That is, we can do some executable actions, which we understand as member methods.

The access and member properties of the member method can be described by reference to the access to the member properties above.

Iv. member methods and member properties also include public,protected,private,static,final and the scope is the same, and then give some examples for your reference and understanding.

The static member method can only access static member properties and member methods, and the class internal access static method can use Self:: Static method (), class external Access using class Name:: Static method ()

1. Custom method:

Class Finalclass {Static $a = ' $a '; const A = ' a ';p ublic $name = ' chicken sister ';p rotected $sex = ' male + female ';p rivate $age = 23; Defines a common method public function Publickfun () {echo ' Finalclass class in self:: $a-and '. Self:: $a. " 

2. Magic method

I. The Magic method must be defined as public, and all other magic methods must be so

Ii. from PHP 5 onwards, classes in PHP can use the Magic method. Its provisions to start with two underscores () methods are preserved as a magic method, it is recommended that the function name is best not to start, unless it is to overload the existing magic method, the next list of some magic methods, if you want to learn more about a particular one can be queried to understand, here is not the introduction of a small space.

1, construct () construction method

Destruct ()

destructor method

2. Clone ()

If you want to copy an object, you need to use the Clone method

3. ToString ()

method is called automatically when an object is converted to a string, such as when using Echo to print an object

4, Sleep () serialization of the time with

Wakeup Crossdress is called when the line is being serialized

5, Set_state ()

This static method is called when Var_export () is called (Valid from PHP 5.1.0)

6. Invoke (PHP 5.3.0 or later)

The Invoke method is called automatically when an attempt is made to invoke an object in a way that invokes a function.

7, Callstatic (PHP 5.3.0 or later) is to handle static method calls

8, get () when an undefined property is called, this method is triggered, and the passed parameter is the name of the property being accessed.

When set () assigns a value to an undefined property, this method is triggered, and the passed parameter is the property name and value that is set. There is no declaration here that the access control is a Proteced,private property (that is, a property without permission access) when using object invocation.

9, Isset () This method is called when the Isset () function is called on an undefined property

Unset () This method is called when the unset () function is called on an undefined property

10. Call ($method, $arg _array)

This method is called when an undefined method is called

The undefined method here includes a method that does not have permission to access, if the method does not exist in the parent class to find this method, if the parent class does not exist in the call () method to invoke this class, if there is no calls () method in this class to find the parent class in the calling () method.

11, AutoLoad () Automatic loading Magic method

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.