Home  >  Article  >  Backend Development  >  How to define classes and their member attributes and operations in PHP_PHP Tutorial

How to define classes and their member attributes and operations in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:39:15927browse

Concept of class: A class is a collection of objects with the same attributes and operations. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: attributes and operations. In object-oriented programming languages, a class is an independent program unit. It should have a class name and include two main parts: attribute description and operation description.


1. Class definition:

i. Class keyword definition uses class


1. Define an empty class

Class Person{};

2. Define a class with member attributes and operations

Class Person{

Member properties.....

Fuck...

}

3. Define a class that cannot be inherited and use the final keyword

Final class Person{

Member properties.....

Fuck...

}

4. Note: The final keyword cannot be used to modify member attributes, but can only modify classes and methods (final methods will be introduced later)

5. Here is a class with final

Define a final class FinalClass, containing a public function

final class FinalClass {

public function ffun() {

echo "This class is final class";

}

}

Define a class ChildFinalClass and inherit the FinalClass class

class ChildFinalClass extends FinalClass {

public function fchildfun() {

echo ‘This class inherits the final class FinalClass’;


}

}

In this way, when executing the above command, the system will prompt


Fatal error: Class ChildFinalClass may not inherit from final class (FinalClass)


Proves that classes defined by the final keyword cannot be inherited by subclasses

2. Definition of member attributes in the class

i. Member attributes are some variable attributes defined for the class. As a class, people have a pair of eyes (normal, except Erlangshen), a mouth, two ears, etc. Fixed proper nouns used to describe or express something are called member attributes

ii. Keywords used in the declaration of member attributes in a class

iii. Common member attribute declarations are composed of the following keywords
It starts with public, var, protected, private, and is followed by a variable. There are also some member attributes including static and constant const.

Public: Represents global, accessible to both internal and external subclasses of the class

Var: In the PHP 5 version, this member attribute will be considered a public type attribute

protected means protected and can only be accessed in this class, subclass or parent class


private
Indicates private and can only be used within this class

Static: 1) Static attributes,

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

3) Static data exists in the data segment in memory when the class is loaded for the first time (initializing the static segment)

4) Use self::member attribute name in the class

                                                                                                                                                                                                         Class name:: Member attribute name

Const: 1) Constant attributes in a class. When declaring constants in a class, you must use const

2) Use SELF :: member attribute name

3) Use outside the class Class name::Member attribute name

Note: The variables in the attributes can be initialized, but the initialized value must be a constant. The constant here refers to the constant that the PHP script is in the compilation stage, not the constant calculated in the running stage after the compilation stage. For example, it cannot contain any operators, cannot be any variables, cannot be a function, etc.

iv. How to call member attributes:

In the member method of the class, you can access the properties and methods of the class through $this->property (property is the property name), but it cannot be used to access the static properties of the class or in the static method. , use self::$property instead. The pseudo variable $this can be used in non-static methods of a class. This pseudo variable is a reference to the instantiated object that calls the method.

Next, use code to explain the above content:

class FinalClass {

//static $a = strTolower();
//This writing is wrong

//const A = 1+2; //This writing is wrong

//public $name = 123+456; 
//This writing is wrong

static $a = '$a';

const A = 'A';

public $name = 'Sister Feng';

protected $sex = 'male+female';

private $age = 23;

}

class ChildFinalClass extends FinalClass{


public function fchildfun(){


echo 'ChildFinalClass::$a->'.ChildFinalClass::$a."


";


Echo 'ChildFinalClass::A->'.ChildFinalClass::A."


";


echo 'Hello everyone, my name is'.$this->name."


";


echo 'I am:'.$this->sex.'生


';


echo 'My year:'.$this->age.'


';

//Since age is a private member attribute, it will not be called here, and it will be prompted that this attribute is not defined in the ChildFinalClass class.

}


}


$obj = new ChildFinalClass();


$obj->fchildfun();

3. Definition of operations in the class

i. I generally like to call operations as member methods. Below I will call operations as methods, but they are all the same

ii. Definition of member methods: Member methods are some function methods defined for the class. For example, take this class as an example. People can eat, run, and type code. These are member methods. That is to say, you can do some executable actions, which we understand as member methods

iii. For access to member methods and member attributes, please refer to the introduction to access to member attributes above.

iv. Member methods and member attributes also include public, protected, private, static, final and the scope is the same. Here are some examples for your reference and understanding.

v. Static member methods can only access static member properties and member methods, and you can use self::static method() to access static methods inside the class, and use class name::static method() to access externally the class


1. Custom method:

class FinalClass {

static $a = '$a';

const A = 'A';

public $name = 'Sister Feng';

protected $sex = 'male+female';

private $age = 23;

//Define a public method

public function publickfun(){


echo 'self::$a->'.self::$a."


";

in the FinalClass class


echo "self::A->".self::A."


";

in FinalClass class


echo 'Hello everyone, my name is:'.$this->name."


";


echo 'I am:'.$this->sex.'生


';


echo 'My year:'.$this->age.'


';

}

//Define a protected method


protected function protectedfun(){


echo 'self::$a->'.self::$a."


";

in the FinalClass class


echo "self::A->".self::A."


";

in FinalClass class


echo 'Hello everyone, my name is:'.$this->name."


";


echo 'I am:'.$this->sex.'生


';


echo 'My year:'.$this->age.'


';


}


//Define a private method

private function privatefun(){

echo 'self::$a->'.self::$a."


";

echo "self::A->".self::A."


";

in FinalClass class

echo 'Hello everyone, my name is:'.$this->name."


";

echo 'I am:'.$this->sex.'生


';

echo 'My year:'.$this->age.'


';


}

}


class ChildFinalClass extends FinalClass{

public function fchildfun(){

echo 'ChildFinalClass::$a->'.ChildFinalClass::$a."


";

Echo 'ChildFinalClass::A->'.ChildFinalClass::A."


";

echo 'I am:'.$this->sex.'生


';

echo 'My year:'.$this->age.'


';

//Since age is a private member attribute, it can be understood that I don’t want others to know my age, so it will not be called here, and it will prompt that this attribute is not defined in the ChildFinalClass class.

$this->publickfun();

$this->protectedfun();

$this->privatefun();   

//Since privatefun is a private member method, it will not be called here.


}

}


$obj = new ChildFinalClass();


$obj->fchildfun();


2.Magic method

i. Magic methods must be defined as public, as must all other magic methods

ii. From PHP 5 and later, classes in PHP can use magic methods. It stipulates that methods starting with two underscores (__) are reserved as magic methods, so it is recommended that everyone’s function names should not start with __, unless it is to overload existing magic methods. Here are some magic methods. If If you want to know more about it, you can inquire about a certain one, so I won’t introduce it in detail here.

1. __construct() construction method


__destruct()

Destruction method

2. __clone()

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

3.__toString()

The

method is automatically called when converting an object into a string, such as when using echo to print the object

4. __sleep() is used when serializing


__wakeup is called during deserialization

5. __set_state()

This static method will be called when var_export() is called (valid since PHP 5.1.0)

6. __invoke (valid for PHP 5.3.0 or above)
When trying to invoke an object as a function, the __invoke method is automatically called.

7. __callStatic (valid for PHP 5.3.0 and above) is to handle static method calls

8. __get() When an undefined attribute is called, this method will be triggered, and the parameter passed is the name of the attribute being accessed.


__set() This method will be triggered when assigning a value to an undefined attribute. The parameters passed are the attribute name and value to be set. The non-declaration here includes attributes whose access control is protected and private (that is, attributes that do not have permission to access) when called using an object.

9. __isset() This method is called when the isset() function is called on an undefined attribute


__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 calling an undefined method

The undefined methods here include methods that do not have permission to access; if the method does not exist, go to the parent class to find the method. If it does not exist in the parent class, call the __call() method of this class. If the __call() method does not exist in this class, go to the __call() method in the parent class.

11. __autoload() automatic loading magic method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/732963.htmlTechArticleThe concept of class: A class is a collection of objects with the same properties and operations. It provides a unified abstract description for all objects belonging to this class, which includes two main attributes and operations...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn