Home  >  Article  >  Backend Development  >  how to learn classes in php

how to learn classes in php

巴扎黑
巴扎黑Original
2017-08-04 16:08:251471browse

Class structure: The internal functions of a class may have three things, namely constants, properties and methods. Functions can be understood as constants, variables and functions outside the class.

The code is as follows:

<?php      
class TEST      
{      
    const NAME = &#39;value&#39;; // 常量      
    public $name = &#39;value&#39;; // 属性      
    public function name() // 方法      
    {      
        echo &#39;value&#39;;      
    }      
}      
?>


In this, properties and methods can use three different keywords: public, protected, and private to set the scope of properties and methods. To further differentiate, properties and methods with the private keyword can only be called by methods in the class in which they are located; properties and methods with the protected keyword can also be called by methods in their own parent class and subclass in addition to themselves. Call; properties and methods with the public keyword can be called from the object after instantiation. The biggest advantage of this is that it adds some descriptive features to all properties and methods, making it easier to organize and organize the structure of the code. . The const keyword is skipped first and discussed together with static later.

The static keyword is another type of keyword that is different from public, protected, and private (so it can be used in conjunction with public, protected, and private):

The code is as follows:

<?php      
class TEST      
{      
    public static function name()       
    {      
        echo &#39;value&#39;;      
    }      
}      
?>


Methods with the static keyword can be called directly through the "::" symbol without instantiating the class, and can be combined with public, protected, and private. It allows the call to distinguish permissions, but it is usually paired with public. The constant keyword const mentioned earlier should be of public static type, so constants can only be called through self::NAME, TEST::NAME, etc. The __construct, __destruct and other methods are all static.

In the structural part of the class, the last two keywords introduced are abstract and final. The abstract keyword indicates that this class must be overridden by its subclasses, and the final keyword indicates that this class must not be overridden by its subclasses. Subclass override, the functions of these two keywords are exactly opposite. Methods with abstract are called abstract methods, and classes with abstract methods are called abstract classes. This will be introduced later.

Use of classes:

There are two main ways to use classes, one is to use the new keyword, the other is to use the "::" symbol:

PHP code

The code is as follows:

<?php      
class TEST      
{      
    public static function name()       
    {      
        echo &#39;value&#39;;      
    }      
}      
//方法1:使用new关键字      
$test = new TEST;      
$test->name();      
//方法2:使用“::”符号      
TEST::name();      
?>



(1): Use the new keyword to become an instantiation. The $test above is an instantiation through the TEST class The generated object, $test->name() is called the name method of the $test object.
(2): When using the new keyword to use a class, you can use $this to refer to the class itself.
(3): The prerequisite for using the "::" symbol is that the method must have the static keyword. When using the new keyword, the method being called must have the public keyword (if a method does not have the public keyword , protected, private, the default is public)
(4): The same class can be instantiated into multiple different objects through the new keyword, but they are isolated from each other; " When the ::" symbol is used, the method is shared between multiple uses:

PHP code

The code is as follows:

<?php      
class TEST1      
{      
    public $name = 0;      
    public function name()       
    {      
        $this->name = $this->name + 1;      
    }      
}      
$test1 = new TEST1;      
$test2 = new TEST1;      
$test1->name(); //$name1 == 1      
$test2->name(); //$name1 == 1      
/*--------------------------------------------*/     
class TEST2      
{      
    public static $name = 0;      
    public static function name()       
    {      
        TEST2::$name = TEST2::$name + 1;      
    }      
}      
TEST2::name(); // $name == 1      
TEST2::name(); // $name == 2      
?>

class Relationship:

The relationship between classes mainly includes abstraction, interface and inheritance:

PHP code

The code is as follows:

<?php      
abstract class TEST1 // 抽象      
{      
    abstract public function name1();      
    public function name2()      
    {      
    }      
}      
class TEST2 extends TEST1 implements TEST3 // 继承      
{      
    public function name1()      
    {      
    }      
}      
interface TEST3 // 接口      
{      
    public function name2();      
}      
?>



(1) A class with the abstract keyword is an abstract class, and a method with the abstract keyword is an abstract method. Abstract methods in abstract classes must be overridden in subclasses.
(2) A class with the interface keyword is an interface. The interface does not allow any methods to be implemented. All methods in the interface must be overridden in subclasses.
(3) Those with the words classA extends classB or classA implements classB are inheritance. extends means inheriting another class, and implements means inheriting another interface. Only one class can be extended at a time, but multiple interfaces can be implemented.
(4) Abstract classes, interfaces, and ultimately inherited and implemented methods must be public.

During the inheritance process, the subclass will override the method of the parent class with the same name. At this time, if you need to call the method of the parent class in the subclass, you can use the parent keyword or the class name plus ":: "Symbol call:

PHP code

The code is as follows:

<?php      
class TEST1 extends TEST2      
{      
    public function name()      
    {      
        echo parent::name2();      
        echo TEST2::name2();      
    }      
}      
class TEST2      
{      
    public function name2()      
    {      
        echo &#39;value2&#39;;      
    }      
}      
$test = new TEST1;      
$test->name();      
?>



Here is another explanation of the "::" method in the class Function, one function is to call constants (actually, it can also be understood as static), static properties and methods without instantiation, and the other function is to establish a convenient calling channel inside the class through self, parent and class name. .

The relationship between objects is mainly "==" equal, "===" all equal, not equal and clone: ​​

PHP code

<?php     
class TEST     
{     
    public function name()     
    {     
    }     
}     
$test1 = new TEST;     
$test2 = new TEST;     
$test3 = $test1;     
echo $test1 == $test2 ? true : false; // true     
echo $test1 == $test3 ? true : false; // true     
echo $test2 == $test3 ? true : false; // true     
echo $test1 === $test2 ? true : false; // false     
echo $test1 === $test3 ? true : false; // true     
echo $test2 === $test3 ? true : false; // false     
?>

(1)两个类只要拥有相同的属性和方法,就是“==”等于。
(2)两个类必须是指向的同一个对象,才能是“===”全等于。

clone比较特殊,在上面的例子中,$test3 = $test1的过程并不是给了 $test3 一份 $test1 对象的拷贝,而是让 $test3 指向了 $test1,如果一定要获得一份$test1的拷贝,就必须使用clone关键字:

PHP代码

 代码如下:

<?php      
$test3 = clone $test1;      
?>

类的钩子:

__autoload:
是一个函数名,也是唯一一个在类的外部使用的钩子,在实例化一个对象的时候,如果没有预先载入类,就会调用这个钩子。

__construct
在类被实例话的时候,被调用的钩子,可以做一些初始化的操作。

__destruct
在类被销毁的时候,被调用的钩子。

__call
当对象试图调用一个不存在的方法的时候,被调用的钩子

__sleep
当使用serialize()函数对一个类进行序列话操作的时候,会调用这个钩子

__wakeup
当使用unserialize()函数对一个类进行反序列话操作的时候,会调用这个钩子

__toString
当一个对象将被转变为字符串的时候,会调用这个钩子(比如echo的时候)

__set_state
当调用var_export()函数操作一个类的时候,会调用这个钩子

__clone
当使用clone关键字对一个类进行拷贝操作的时候,会调用这个钩子

__get
在获取一个类中的属性值的时候,会调用这个钩子

__set
在设置一个类中的属性值的时候,会调用这个钩子

__isset
在使用isset()函数对类中的属性值进行判定的时候,会调用这个钩子

__unset
在使用unset()函数销毁一个属性值的时候,会调用这个钩子

类的小技巧:

在实例话一个类的时候,可以使用这样的形式给__construct钩子传递参数:

PHP代码 

代码如下:

<?php      
class TEST      
{      
    public function __construct($para)      
    {      
        echo $para;      
    }      
}      
$test = new TEST(&#39;value&#39;); // 显示 value      
?>


foreach()函数可以用来对类或者对象中的属性进行遍历,遍历的时候会先判断public, protected, private的情况而显示:

PHP代码 

代码如下:

<?php      
class TEST      
{      
    public $property1 = &#39;value1&#39;;      
    public $property2 = &#39;value2&#39;;      
    public $property3 = &#39;value3&#39;;      
    public function name()      
    {      
        foreach($this as $key => $value)      
        {      
            print "$key => $value\n";      
        }      
    }      
}      
$test = new TEST;      
foreach($test as $key => $value)      
{      
    print "$key => $value\n";      
}      
$test->name();      
?>


在给类中的方法传递参数的时候,可以对参数进行强制的判定,这里只支持对数组和对象的判定:

PHP代码

代码如下:

<?php      
class TEST1      
{      
    public function name( TEST2 $para )      
    {      
    }      
}      
class TEST2      
{      
}      
$test2 = new TEST2;      
$test1 = new TEST1;      
$test1->name(&#39;value&#39;); // 会报错,因为这个参数必须是TEST2实例化以后的对象      
$test1->name($test1); // 不会报错      
?>


兼容php4的语法:

php5的类是往下兼容php4的,这些php4时代的语法也得到了继承,但是并不建议在php5的环境中使用。

(1)使用var预设属性,会自动转换成public。

(2)使用类名作为构造函数,在没有__construct构造方法的情况下,会寻找和类名相同的函数作为构造函数。

The above is the detailed content of how to learn classes in php. For more information, please follow other related articles on the PHP Chinese website!

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