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?
php interview questions: object-oriented questions
1. Write the differences between PHP’s three access control modes: public, protected, and private (Sina Technology Department)
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
Related questions: Please write the PHP5 permission control modifier
private protected public
2. Design pattern inspection: Please use the monomorphic design pattern method to design the class To meet the following requirements:
Please use PHP5 code to write classes to achieve that only one database connection can be obtained during each access to the database connection. The detailed code of Connecting to the database is ignored. Please write the main logic code (Sina Technology Department)
<?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(); ?>
3. Write the output result of the following program (Sina Technology Department)
<?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
[!]4. What are the magic methods functions in PHP5? Please give examples of their usage (Tencent PHP engineer written test questions)
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;
?>
Double md5 encryption6. How would you declare a class named “myclass” with no methods or properties? (Yahoo) class myclass{};
Related questions: How to declare a class named “myclass” with no methods or properties? 7. How would you create an object, which is an instance of “myclass”? (Yahoo) $obj= new myclass();
Related questions: How to instantiate an object named “myclass”? 8. How do you access and set properties of a class from within the class? (Yahoo) Use statement: $this->propertyName, for example: <?php class mycalss{ private $propertyName; public function construct() { $this->propertyName = "value"; } } ?>9. The code below _ because .(Tencent)
<?php
class Foo{
?>
<?php
function bar(){
print "bar";
}
}
?>
A. will work, class definitions can be split up into multiple PHP blocks.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
serialize() and unserialize()
11. In PHP, if the derived class has a function with the same name as the parent class, the function of the derived class will replace the function of the parent class, and the program result is<?php
class A{
function disName(){
echo "Picachu";
}
}
class B extends A{
var $tmp;
function disName(){
echo "Doraemon";
}
}
$cartoon = New B;
$cartoon->disName();
?>
A. tmpB. 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。
13. 类中如何定义常量、如何类中调用常量、如何在类外调用常量。
类中的常量也就是成员常量,常量就是不会改变的量,是一个恒值。定义常量使用关键字 const,例如:const PI = 3.1415326;
无论是类内还是类外,常量的访问和变量是不一样的,常量不需要实例化对象,访问常量的格式都是类名加作用域操作符号(双冒号)来调用,即:类名:: 类常量名
。
14. autoload()函数是如何运作的?
使用这个魔术函数的基本条件是类文件的文件名要和类的名字保持一致。
当程序执行到实例化某个类的时候,如果在实例化前没有引入这个类文件,那么就自动执行autoload()
函数。
这个函数会根据实例化的类的名称来查找这个类文件的路径,当判断这个类文件路径下确实存在这个类文件后就执行 include 或者 require 来载入该类,然后程序继续执行,如果这个路径下不存在该文件时就提示错误。
15. 哪种OOP设置模式能让类在整个脚本里只实例化一次?(奇矩互动)
A. MVC
B. 代理模式
C. 状态模式
D. 抽象工厂模式
E. 单件模式
答案:E
16. 借助继承,我们可以创建其他类的派生类。在PHP中,子类最多可以继承几个父类?(奇矩互动)
A. 1个
B. 2个
C. 取决于系统资源
D. 3个
E. 想要几个有几个
答案:A
17. 执行以下代码,输出结果是(奇矩互动)
<?php abstract class a{ function construct() { echo "a"; } } $a = new a(); ?>
A. a
B. 一个错误警告
C. 一个致命性的报错
答案:C 因为类a是抽象类,不能被实例化
18. 执行以下代码,输出结果是
<?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()
来显示调用父类构造函数。
19. 请定义一个名为MyClass的类,这个类只有一个静态方法justDoIt。(卓望)
<?php class MyClass{ public static function justDoIt(){ } } ?>
20. 只有该类才能访问该类的私有变量吗?(卓望)
是的
21. 写出你知道的几种设计模式,并用php代码实现其中一种。(卓望)
单例模式,工厂模式
单例模式 实现代码 见 第二题
总结:
我们在这个片文章中,我们主要给大家汇总了一下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!

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.