The following is a tutorial on PHP classes and method keywords that I have compiled for you. Interested students can take a look.
1. final
If we don’t want a class to be inherited, we use final to modify the class. This class will not be inherited.
final---used before classes and methods.
Final class---cannot be inherited.
Final method---cannot be overridden.
2. Public means global, and can be accessed by subclasses inside and outside the class; private means private, and can only be used within this class; protected means protected, and can only be accessed by this class, subclasses, or parent classes;
3. This is a pointer to the current object (can be regarded as a pointer in C), self is a pointer to the current class, parent is a pointer to the parent class
self: When variables and methods are set to static, If you use self
4 or static
to declare a class member or method as static in this class, you can directly access it without instantiating the class. You cannot access the static members (except static methods) through an object. Static members belong to the class and do not belong to any object instance, but object instances of the class can be shared.
Then let’s take a look at the difference between using $object->… and using class::…:
1. When using $object->…, you need to execute the constructorCreate objects;
2. Use class::... to call static methods/variables, without executing the constructor to create objects;
3. Use class::... to call non-static methods/variables, also There is no need to execute a constructor to create an object.
5. abstract
1. Abstract classabstract class
1. An abstract class refers to a class with the abstract keyword added before class and an abstract method (abstract keyword added before the class method function keyword).
2 . Abstract classes cannot be instantiated directly. The abstract class only defines (or partially implements) the methods required by the subclass. Subclasses can make an abstract class concrete by inheriting it and by implementing all the abstract methods in the abstract class.
3. If a subclass needs to be instantiated, it must implement all abstract methods in the abstract class. If the subclass does not implement all abstract methods in the abstract class, then the subclass is also an abstract class and must be preceded by the abstract keyword in class and cannot be instantiated.
6、interface interface
1. Abstract classes provide standards for concrete implementation, while interfaces are pure templates. Interfaces only define functions, not implementation content. Interfaces are declared with the keyword interface.
2 . Interface is completely abstract. It can only declare methods, and only public methods. It cannot declare private and protected methods, cannot define method bodies, and cannot declare instance variables. However, interfaces can declare constant variables. But placing constant variables in an interface violates its purpose of existing as an interface, and also confuses the different values of interfaces and classes. If you really need it, you can put it in the corresponding abstract class or Class.
7, instanceof
Another new member of PHP5 is the instdnceof keyword. Use this keyword to determine whether an object is an instance of a class, a subclass of a class, or implements a specific interface, and perform corresponding operations. In some cases, we want to determine whether a class is of a specific type, or implements a specific interface. instanceofoperator is very suitable for this task. The instanceof operator checks three things: whether the instance is of a specific type, whether the instance inherits from a specific type, and whether the instance or any of its ancestor classes implements a specific interface. For example, suppose you want to know whether an object named manager is an instance of class Employee:
$manager = new Employee(); if ($manager instanceof Employee) echo "Yes"; 有两点值得注意。首先,类名没有任何定界符(引号)。使用定界符将导致 语法错误 。其次,如果比较失败,脚本将退出执行。instanceof关键字在同时处理多个对象时特别有用。例如,你可能要重复地调用某个函数,但希望根据对象类型调整函数的行为。可以使用case语句和instanceof关键字来实现这个目标。 class test{} class test{} class testChilern Extends test{} $a = new test(); $m = new test(); $i = ($m instanceof test); if($i) echo '$m是类test的实例!<br />'; // get this value switch ($a instanceof test){ case true : echo 'YES<br />'; break; case false : echo 'No<br />'; //get this value break; } $d=new testChilern(); if($d instanceof test)echo '$d是类test的子类!<br />'; // get this value
What is the role of instanceof in php
Function: (1) Determine whether an object is of a certain class Instance, (2) Determine whether an object implements a certain interface.
First usage:
<?php $obj = new A(); if ($obj instanceof A) { echo 'A'; }
Second usage:
<?php interface ExampleInterface { public function interfaceMethod(); } class ExampleClass implements ExampleInterface { public function interfaceMethod() { return 'Hello World!'; } } $exampleInstance = new ExampleClass(); if($exampleInstance instanceof ExampleInterface){ echo 'Yes, it is'; }else{ echo 'No, it is not'; } ?>
Output result: Yes, it is
In addition, please pay attention to instanceof and is_subclass_of (), please look at the code:
<?php class Foo { public $foobar = 'Foo'; public function test() { echo $this->foobar . "\n"; } } class Bar extends Foo { public $foobar = 'Bar'; } $a = new Foo(); $b = new Bar(); echo "use of test() method\n"; $a->test(); $b->test(); echo "instanceof Foo\n"; var_dump($a instanceof Foo); // TRUE var_dump($b instanceof Foo); // TRUE echo "instanceof Bar\n"; var_dump($a instanceof Bar); // FALSE var_dump($b instanceof Bar); // TRUE echo "subclass of Foo\n"; var_dump(is_subclass_of($a, 'Foo')); // FALSE var_dump(is_subclass_of($b, 'Foo')); // TRUE echo "subclass of Bar\n"; var_dump(is_subclass_of($a, 'Bar')); // FALSE var_dump(is_subclass_of($b, 'Bar')); // FALSE ?> 输出结果(PHP 5.4.4): use of test() method Foo Bar instanceof Foo bool(true) bool(true) instanceof Bar bool(false) bool(true) subclass of Foo bool(false) bool(true) subclass of Bar bool(false)
The above is a tutorial I compiled to explain PHP classes and method keywords. I hope it will be helpful to you in the future.
Related articles:
Specific usage methods of namespace and use
PHP closure function() use() Detailed usage guide
Detailed usage guide for PHP namespace namespace and import use
The above is the detailed content of Detailed explanation of PHP class and method keyword tutorials. For more information, please follow other related articles on the PHP Chinese website!