每个优秀的PHP开发人员都应该回答的问题 [英] Questions every good PHP Developer should be able to answer

查看:63
本文介绍了每个优秀的PHP开发人员都应该回答的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历所有好的.Net问题开发人员应该能够回答,并且对该问题的内容和方法印象深刻,因此,本着同样的精神,我向PHP开发人员提出了这个问题.

I was going through Questions every good .Net developer should be able to answer and was highly impressed with the content and approach of this question and so in the same spirit, I am asking this question for PHP Developer.

哪些问题,您认为一个好的 PHP程序员应该能够回答吗?

编辑:我将此问题标记为社区Wiki,因为它不是特定于用户的,并且旨在为整个编程社区提供服务.

EDIT : I am marking this question as community wiki as it is not user specific and it aims to serve programming community at large.

期待一些令人惊奇的回应.

Looking forward for some amazing responses.

注意:也请按照评论中的建议回答问题,以便人们也可以学习有关该语言的新知识.

NOTE : Please answer questions too as suggested in the comments so that people could learn something new too regarding the language.

推荐答案

诚然,我从其他地方偷了这个问题(不记得我在哪里读了),但认为这很有趣:

Admittedly, I stole this question from somewhere else (can't remember where I read it any more) but thought it was funny:

问:什么是T_PAAMAYIM_NEKUDOTAYIM?
A:其范围解析运算符(双冒号)

Q: What is T_PAAMAYIM_NEKUDOTAYIM?
A: Its the scope resolution operator (double colon)

经验丰富的PHP'er会立即知道它的含义. 经验不足(不是希伯来语的)开发人员可能想阅读

An experienced PHP'er immediately knows what it means. Less experienced (and not Hebrew) developers may want to read this.

但现在有更严重的问题:

But more serious questions now:

问:出现此警告的原因是:警告:无法修改标题信息-已发送标题",并且有什么好的方法来防止出现这种情况?
A: 原因:正文数据已发送,导致标头也被发送.
预防措施:在输出任何正文数据之前,请务必先执行标头特定的代码.确保您没有意外发送空格或任何其他字符.

Q: What is the cause of this warning: 'Warning: Cannot modify header information - headers already sent', and what is a good practice to prevent it?
A: Cause: body data was sent, causing headers to be sent too.
Prevention: Be sure to execute header specific code first before you output any body data. Be sure you haven't accidentally sent out whitespace or any other characters.

问:这个查询是什么问题:"SELECT * FROM table WHERE id = $_POST[ 'id' ]"?
A: 1.它很容易受到SQL注入的影响.切勿在查询中直接使用用户输入.首先将其消毒.最好使用准备好的语句( PDO ) 2.不要选择所有列(*),而是指定每一个列.这主要是为了防止例如在将来某个时候添加BLOB列时查询占用内存.

Q: What is wrong with this query: "SELECT * FROM table WHERE id = $_POST[ 'id' ]"?
A: 1. It is vulnarable to SQL injection. Never use user input directly in queries. Sanitize it first. Preferebly use prepared statements (PDO) 2. Don't select all columns (*), but specify every single column. This is predominantly ment to prevent queries hogging up memory when for instance a BLOB column is added at some point in the future.

问: if语句有什么问题:if( !strpos( $haystack, $needle ) ...?
A: strpos返回它第一次发现$ needle的位置的索引位置,该位置可能是0.由于0也解析为false,因此解决方案是使用严格的比较:if( false !== strpos( $haystack, $needle )...

Q: What is wrong with this if statement: if( !strpos( $haystack, $needle ) ...?
A: strpos returns the index position of where it first found the $needle, which could be 0. Since 0 also resolves to false the solution is to use strict comparison: if( false !== strpos( $haystack, $needle )...

问::编写此if语句的首选方式是什么,为什么?
if( 5 == $someVar )if( $someVar == 5 )
A:前者,因为当您忘记使用2个等号($someVar = 5)时,它会防止意外将5分配给$ someVar并会导致错误,而后者不会.

Q: What is the preferred way to write this if statement, and why?
if( 5 == $someVar ) or if( $someVar == 5 )
A: The former, as it prevents accidental assignment of 5 to $someVar when you forget to use 2 equalsigns ($someVar = 5), and will cause an error, the latter won't.

问:给出此代码:

function doSomething( &$arg )
{
    $return = $arg;
    $arg += 1;
    return $return;
}

$a = 3;
$b = doSomething( $a );

...函数调用后$a$b的值是什么?为什么?
A: $a4,而$b3.前者是因为$ arg是通过引用传递的,后者是因为函数的返回值是参数的初始值的副本(而不是对参数的初始值的引用).

...what is the value of $a and $b after the function call and why?
A: $a is 4 and $b is 3. The former because $arg is passed by reference, the latter because the return value of the function is a copy of (not a reference to) the initial value of the argument.

面向对象的操作

问:在类定义中publicprotectedprivate有什么区别?
A: public使类成员可用于所有人",protected使类成员仅对自身和派生类可用,private使类成员仅对类本身可用

Q: What is the difference between public, protected and private in a class definition?
A: public makes a class member available to "everyone", protected makes the class member available to only itself and derived classes, private makes the class member only available to the class itself.

问:这段代码有什么问题:

Q: What is wrong with this code:

class SomeClass
{
    protected $_someMember;

    public function __construct()
    {
        $this->_someMember = 1;
    }

    public static function getSomethingStatic()
    {
        return $this->_someMember * 5; // here's the catch
    }
}

A:静态方法无权访问$ this,因为可以在不实例化类的情况下执行静态方法.

A: Static methods don't have access to $this, because static methods can be executed without instantiating a class.

问::接口和抽象类之间有什么区别?
A:接口定义了实现类is和调用该接口的对象之间的协定.抽象类为将扩展它的类预定义了某些行为.在某种程度上,这也可以视为合同,因为它可以保证存在某些方法.

Q: What is the difference between an interface and an abstract class?
A: An interface defines a contract between an implementing class is and an object that calls the interface. An abstract class pre-defines certain behaviour for classes that will extend it. To a certain degree this can also be considered a contract, since it garantuees certain methods to exist.

问:主要定义getter和setter,直接映射到其内部成员而实际上没有执行行为的方法的类又有什么问题呢?
A:这可能是代码的味道,因为该对象充当了高贵的数组,没有太多其他用途.

Q: What is wrong with classes that predominantly define getters and setters, that map straight to it's internal members, without actually having methods that execute behaviour?
A: This might be a code smell since the object acts as an ennobled array, without much other use.

问:为什么PHP使用接口实现时不是最优的?
A: PHP不允许您定义方法的预期返回类型,这实际上使接口变得毫无用处. :-P

Q: Why is PHP's implementation of the use of interfaces sub-optimal?
A: PHP doesn't allow you to define the expected return type of the method's, which essentially renders interfaces pretty useless. :-P

这篇关于每个优秀的PHP开发人员都应该回答的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆