Home > Backend Development > PHP Tutorial > Analysis of object-oriented questions in PHP interview questions

Analysis of object-oriented questions in PHP interview questions

黄舟
Release: 2023-03-17 09:36:01
Original
4105 people have browsed it

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();
?>
Copy after login
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();
?>
Copy after login

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 calledcall 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

Related questions: Please write the constructor and destructor of php5
Constructor:

constructDestructor:
destruct

5. How to use the following class and explain what it means?
<?php
    class test{
        function Get_test($num){
            $num = md5(md5($num)."En");
            return $num;
        }
    }

    $testObject = new test();
    $encryption = $testObject->Get_test("itcast");
    echo $encryption;
?>
Copy after login
Double md5 encryption

6. How would you declare a class named “myclass” with no methods or properties? (Yahoo)
class myclass{};
Copy after login
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();
Copy after login
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";
        }
    }
?>
Copy after login

9. The code below _ because .(Tencent)
<?php
class Foo{
?>
<?php
    function bar(){
        print "bar";
    }
}
?>
Copy after login
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

10. The attributes of a class can be serialized and saved in the session, so that the entire class can be restored later. The function to be used is.
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();
?>
Copy after login
A. tmp

B. Picachu
C. disName
D. Doraemon
E. No output
Answer: D

12. Interfaces and abstractions What is the difference between classes?

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();
?>
Copy after login

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();
?>
Copy after login

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(){

    }
}
?>
Copy after login
20. 只有该类才能访问该类的私有变量吗?(卓望)

是的

21. 写出你知道的几种设计模式,并用php代码实现其中一种。(卓望)

单例模式,工厂模式
单例模式 实现代码 见 第二题

总结:

我们在这个片文章中,我们主要给大家汇总了一下php面试题中关于面向对象中的一些常见面试问题,具体细节大家可以自己扩展,希望对你有所帮助!

相关推荐:

最让人容易出错的10道php面试题


Sharing of php core technology issues in php interview questions


Summary of written test questions in php interview questions


The most complete summary of PHP interview questions and answers in 2017


##

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template