Home >Backend Development >PHP Tutorial >PHP Class and Object Study Notes_PHP Tutorial

PHP Class and Object Study Notes_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:50:39768browse

A learning note on PHP Class classes and objects written in my study. I will share it with my friends below. I hope this tutorial will be helpful to you in learning classes and objects.

My notes are for reference. If there are any mistakes, please point them out. Thank you

The code is as follows Copy code

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

Class object instance description (common class)

*******************************************/
class ClassDemo{
public $PublicVar;

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

protected $ProtectedVar;//Protected variables are not accessible from outside and subclasses

public static $StaticVar=0;//static 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 don’t want to modify, consider using const to make it a constant, using class name::constant name; interface name::constant
/*
1. Constants need to be assigned an initial value when they are defined
2. Constants cannot be modified.
​ 3. The constant name cannot contain $, it is usually uppercase, and the default is public
4. Constants are used inside the class self::name of constant classname::name of constant
*/
public function __construct( /*$name*/ ){//This can take parameters. After taking parameters, you must also have parameters when creating, otherwise an error will 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 (appears in PHP5)
If you want the method not to be overwritten by other classes when inheriting, you can use final
 
When this keyword is used to modify a class, the class will not 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 necessarily contain abstract methods.
In other words, an abstract class can have no abstract method
     
3. Once the abstract method is included, the class must
Declared as abstract

4. Abstract classes cannot have function bodies

5. If a class inherits an abstract class, it must implement
All abstract methods of this abstract class. (Unless it is also declared as an abstract class itself)
     
****************************************/
}

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

Class object instance description (inherited class)

*******************************************/
class DemoTwo extends ClassDemo{

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

Class object instance description (interface)

*******************************************/
interface Face{
/**********************************
1. When a class implements an interface, the class is required to implement all methods of this interface
2. Interface methods cannot have method bodies
3. Cannot instantiate an interface
4. There can be attributes in the interface, but they must be constants and public

When to use interfaces

1. Set standards
2. Set specifications and let other programmers implement them, such as:

************************************/
public function Name();

}

interface Face2 extends Face{
/*********************************

Inherited interface
When inheriting an interface, you do not need to implement the methods of the parent interface

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

const namevar=20;

}

class Demo implements Face2{
/***********************************

Implementing interfaces can implement multiple interfaces at the same time
When a class implements certain interfaces, it must implement all interface methods

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

public $Name1=0;

public function Name(){

echo Face2::namevar;

}

}

?>

Summary

To access static member attributes or methods inside a class, use self:: (note not $slef), such as:

The code is as follows
 代码如下 复制代码

slef:: $country
slef:: myCountry()

Copy code

 代码如下 复制代码

parent:: $country
parent:: myCountry()

slef:: $country

slef:: myCountry()

 代码如下 复制代码

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

To access parent class static member properties or methods in a subclass, use parent:: (note not $parent), such as:

The code is as follows Copy code

parent:: $country
parent:: myCountry()




External access to static member attributes and methods is class name/subclass name::, such as:

•Class/Object Function
The code is as follows
Copy code


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




But static methods can also be accessed through ordinary objects.
•__autoload — Attempts to load an undefined class •call_user_method_array — Call a user method, passing an array of parameters (deprecated) •call_user_method — Call a user method on a specific object (deprecated) •class_alias — Create an alias for a class •class_exists — Check if a class is defined •get_called_class — The name of the "Late Static Binding" class •get_class_methods — Returns an array consisting of class method names •get_class_vars — Returns an array consisting of the default attributes of a class •get_class — Returns the class name of the object •get_declared_classes — Returns an array consisting 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 associative array of object properties •get_parent_class — Returns the parent class name of an object or class •interface_exists — Check if the interface has been defined •is_a — Returns TRUE if the object belongs to this class or this class is the parent class of this object •is_subclass_of — Returns TRUE if this object is a subclass of this class •method_exists — Check whether a class method exists •property_exists — Check if an object or class has the property •trait_exists — Check if the specified trait exists

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632631.htmlTechArticleA PHP Class class and object learning notes written in the study, share it with your friends below, I hope this tutorial It will be helpful for you to learn classes and objects. Personal notes....for borrowing...
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