This article introduces the basic knowledge of classes and objects in the PHP programming language, which is suitable for beginners to read and hope to help you.
PHP 5 introduces a new object model. Completely rewrite the way PHP handles objects, allowing for better performance and more features.
First, the basic concept
1. Class
The definition of each class begins with the keyword class followed by the class name, which can be the name of any non-PHP reserved word. Followed by a pair of curly braces containing the definitions of the class members and methods. The pseudo-variable $this can be used when a method is called inside an object. A $this is a reference to the calling object (usually the object to which the method belongs, but it can also be another object if the method is called statically from within the second object). Look at the following example:
Example#1 $this variables in object-oriented languages
<?PHPclassA {functionfoo () {if(isset($this)) { Echo' $this is defined ('; Echo Get_class($this); Echo") \ n"; } Else { Echo"\ $this is not defined.\n"; } } } classB {functionBar () {A::foo (); } } $a=NewA ();$a-foo (); A::foo ();$b=NewB ();$b-Bar (); B::bar ();?>
The example above will output:
- $this is defined (a)
- $this is not defined.
- $this is defined (b)
- $this is not defined.
Example#2 a simple class definition
<? PHP class Simpleclass { // member declaration public $var = ' A default value '; // method declaration public function Displayvar () { echo$thisvar; ? >
Default values for Example#3 class members
<?PHPclassSimpleclass {//Invalid class member definition: Public $var 1= ' Hello '. ' World '; Public $var 2= <<<Eodhello Worldeod; Public $var 3= 1 + 2; Public $var 4= Self::Mystaticmethod (); Public $var 5=$myVar; //the correct class member definition: Public $var 6=myconstant; Public $var 7= Self::classconstant; Public $var 8=Array(true,false);} ?>
2. New
To create an instance of an object, you must create a new object and assign it to a variable. When a new object is created, the object is always assigned, unless the object defines the constructor and throws an exception when an error occurs.
Example#4 Creating an instance
<? PHP $instance New Simpleclass ();
Copy code when you assign an instance of an object that has already been created to a new variable, the new variable accesses the same instance and assigns a value to that object. This behavior is the same as when the function is passed into the instance. You can use clones to create a new instance of an object that has already been created.
Assigning values to example#5 objects
<?PHP$assigned=$instance;$reference= &$instance;$instance-var= ' $assigned'll has this value ';$instance=NULL;//$instance and $reference become nullVar_dump($instance);Var_dump($reference);Var_dump($assigned);?>
The copy code example will output:
- Null
- Null
- Object (Simpleclass) #1 (1) {
- ["var"]=>
- String ("$assigned would have the this value"
- }
3, extends
A class can inherit the methods and members of another class in a declaration with the extends keyword. You cannot extend multiple classes, only one base class can be inherited.
Inherited methods and members can be overwritten by a re-declaration with the same name, unless the parent class defines the method with the final keyword. The overridden method or member can be accessed through the parent::.
Example#6 Simple class inheritance
<? PHP class extends Simpleclass { // Redefine The parent method function Displayvar () { c10/>echo "Extending class\n"; :: Displayvar (); $extended New Extendclass (); $extended -Displayvar ();? >
The example above will output:
- Extending class
- A default value
Learning classes and objects for beginners in PHP (1)