Table of Contents
php interview questions: object-oriented questions
1. Write the differences between PHP’s three access control modes: public, protected, and private (Sina Technology Department)
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:
Connecting to the database is ignored. Please write the main logic code (Sina Technology Department) " > 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)
3. Write the output result of the following program (Sina Technology Department)
magic methods functions in PHP5? Please give examples of their usage (Tencent PHP engineer written test questions) " > [!]4. What are the magic methods functions in PHP5? Please give examples of their usage (Tencent PHP engineer written test questions)
class myclass{};
Copy after login
" >
class myclass{};
Copy after login
$obj= new myclass();
Copy after login
" >
$obj= new myclass();
Copy after login
serialize() and unserialize()
13. 类中如何定义常量、如何类中调用常量、如何在类外调用常量。
14. autoload()函数是如何运作的?
15. 哪种OOP设置模式能让类在整个脚本里只实例化一次?(奇矩互动)
16. 借助继承,我们可以创建其他类的派生类。在PHP中,子类最多可以继承几个父类?(奇矩互动)
17. 执行以下代码,输出结果是(奇矩互动)
18. 执行以下代码,输出结果是
19. 请定义一个名为MyClass的类,这个类只有一个静态方法justDoIt。(卓望)
20. 只有该类才能访问该类的私有变量吗?(卓望)
21. 写出你知道的几种设计模式,并用php代码实现其中一种。(卓望)
Home Backend Development PHP Tutorial Analysis of object-oriented questions in PHP interview questions

Analysis of object-oriented questions in PHP interview questions

Nov 10, 2017 am 10:38 AM
php object

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

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles