Object-oriented is an indispensable part of our learning PHP. Many friends have only a little knowledge of object-oriented. Therefore, when many friends go to the company for interviews, they do not know the object-oriented questions in the PHP interview questions. What should we do? In the previous article, we also introduced PHP interview questions, written test questions, and PHP core technical questions. Today we will take you to see what are the object-oriented questions in this PHP interview question?
public : Public, can be accessed anywhere
protected: Inherited, can only be accessed in this class or subclass, access is not allowed elsewhere
private: Private, can only be accessed in this class, elsewhere Access not allowed
<?php class Mysql { private static $instance = null; private $conn; // 构造方法,设置为private,不允许通过new获得对象实例 private function construct(argument) { $conn = mysql_connect("localhost","root","root"); } // 获取实例方法 public function getInstance() { if (!self::$instance instanceof self) { self::$instance = new self; } return self::$instance; } // 禁止克隆 private function clone(){} } // 获得对象 $db = Mysql::getInstance(); ?>
<?php class a { protected $c; public function a() { $this->c = 10; } } class b extends a { public function print_data() { return $this->c; } } $b = new b(); echo $b->print_data(); ?>
Output result 10
sleep
serialize was previously used wakeup
Called when unserialize is called toString
Called when printing an object set_state
Called when calling var_export, use ## The return value of #set_state is used as the return value of var_export
construct
Constructor function is called when instantiating the object
destruct Destruction Function, called when the object is destroyed
call The object calls a method. If the method exists, it is called directly. If it does not exist, it is called
call Function
get When reading the properties of an object, if the property exists, it will be returned directly. If it does not exist, the
get function
set will be called to set the property of an object. Attribute, if the attribute exists, the value is assigned directly. If it does not exist, the
set function is called.
isset is called when detecting whether the attribute of an object exists
unset Called when unsetting the properties of an object
clone Called when cloning an object
autoload When instantiating an object, if the corresponding class does not exist , then the method is called
constructDestructor:
destruct
<?php class test{ function Get_test($num){ $num = md5(md5($num)."En"); return $num; } } $testObject = new test(); $encryption = $testObject->Get_test("itcast"); echo $encryption; ?>
class myclass{};
$obj= new myclass();
<?php class mycalss{ private $propertyName; public function construct() { $this->propertyName = "value"; } } ?>
<?php class Foo{ ?> <?php function bar(){ print "bar"; } } ?>
B. will not work, class definitions must be in a single PHP block.
C. will not work, class definitions must be in a single file but can be in multiple PHP blocks.
D. will work, class definitions can be split up into multiple files and multiple PHP blocks .
Answer: B
<?php class A{ function disName(){ echo "Picachu"; } } class B extends A{ var $tmp; function disName(){ echo "Doraemon"; } } $cartoon = New B; $cartoon->disName(); ?>
B. Picachu
C. disName
D. Doraemon
E. No output
Answer: D
Abstract class is a class that cannot be instantiated and can only be used as a parent class of other classes. Abstract classes are declared using the keyword abstract. Abstract classes are similar to ordinary classes, including member variables and member methods. The difference between the two is that an abstract class must contain at least one abstract method. An abstract method has no method body. This method is inherently to be overridden by subclasses. .
The format of abstract method is: abstract function abstractMethod();
接口是通过 interface 关键字来声明的,接口中的成员常量和方法都是 public 的,方法可以不写关键字 public,接口中的方法也是没有方法体。接口中的方法也天生就是要被子类实现的。
抽象类和接口实现的功能十分相似,最大的不同是接口能实现多继承。在应用中选择抽象类还是接口要看具体实现。
子类继承抽象类使用 extends,子类实现接口使用 implements。
类中的常量也就是成员常量,常量就是不会改变的量,是一个恒值。定义常量使用关键字 const,例如:const PI = 3.1415326;
无论是类内还是类外,常量的访问和变量是不一样的,常量不需要实例化对象,访问常量的格式都是类名加作用域操作符号(双冒号)来调用,即:类名:: 类常量名
。
使用这个魔术函数的基本条件是类文件的文件名要和类的名字保持一致。
当程序执行到实例化某个类的时候,如果在实例化前没有引入这个类文件,那么就自动执行autoload()
函数。
这个函数会根据实例化的类的名称来查找这个类文件的路径,当判断这个类文件路径下确实存在这个类文件后就执行 include 或者 require 来载入该类,然后程序继续执行,如果这个路径下不存在该文件时就提示错误。
A. MVC
B. 代理模式
C. 状态模式
D. 抽象工厂模式
E. 单件模式
答案:E
A. 1个
B. 2个
C. 取决于系统资源
D. 3个
E. 想要几个有几个
答案:A
<?php abstract class a{ function construct() { echo "a"; } } $a = new a(); ?>
A. a
B. 一个错误警告
C. 一个致命性的报错
答案:C 因为类a是抽象类,不能被实例化
<?php class a{ function construct(){ echo "echo class a something"; } } class b extends a{ function construct(){ echo "echo class b something"; } } $a = new b(); ?>
A. echo class a something echo class b something
B. echo class b something echo class a something
C. echo class a something
D. echo class b something
答案:D
类 b 继承自类 a,两个类都定义了构造函数,由于二者名字相同,所以子类中的构造函数覆盖了父类的构造函数,要想子类对象实例化时也执行父类的构造函数,需要在子类构造函数中使用 parent::construct()
来显示调用父类构造函数。
<?php class MyClass{ public static function justDoIt(){ } } ?>
是的
单例模式,工厂模式
单例模式 实现代码 见 第二题
总结:
我们在这个片文章中,我们主要给大家汇总了一下php面试题中关于面向对象中的一些常见面试问题,具体细节大家可以自己扩展,希望对你有所帮助!
相关推荐:
##
The above is the detailed content of Analysis of object-oriented questions in PHP interview questions. For more information, please follow other related articles on the PHP Chinese website!