用 PHP 学习 OO 编码,静态 != 表达式,但 PHP 手册说所有有值的都是表达式,困惑 [英] Learning OO coding with PHP, static != expressions, but PHP manual says everything that has a value is an expression, confused

查看:22
本文介绍了用 PHP 学习 OO 编码,静态 != 表达式,但 PHP 手册说所有有值的都是表达式,困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前我开始学习 OO,我对程序编码很满意,但显然这还不够,我想成为一名拥有丰富经验和知识的精通编码员,所以首先要完全学习必须是面向对象的,然后是我认为正确的设计模式.

I started to learn OO a few days back, I'm quite OK with procedural coding but obviously that is not enough and I want to become a well versed coder with a lot of experience and knowledge, so first thing to learn completely must be OO followed by the right design patterns I think.

无论如何,我有一件事情被卡住了,但我不太了解......

Anyhow, I have one thing where I'm stuck which I don't quite follow...

静态变量...我知道,即使包含函数执行完毕,静态变量也不会丢失其值,并且如果再次执行相同的函数,再次执行等,静态变量将保持其值.

Static variables... I understand that a static variable doesn't lose it's value even if the containing function is finished executing, and will keep it's value if the same function is executed again, and again etc etc.

但我不明白的是,您现在可以为静态变量分配什么?关于 stackoverflow 状态的手册和无数问题,您无法将表达式分配给静态变量.

But what I don't understand is what exactly can you now assign to a static variable? The manual and also countless question on stackoverflow state you can not assign an expression to a static variable.

所以我阅读了 PHP 手册,以找出究竟什么是表达式?手册的答案是(我引用):

So I read the PHP manual, to find out exactly what is considered an expression? The manuals answer is (and I quote):

在 PHP 中,您编写的几乎任何东西都是表达式.定义表达式的最简单但最准确的方法是任何具有值的东西"."

"In PHP, almost anything you write is an expression. The simplest yet most accurate way to define an expression is "anything that has a value"."

当您输入$a = 5"时,您将 '5' 赋值给 $a.显然,'5' 的值为 5,或者换句话说,'5' 是一个表达式"

"When you type "$a = 5", you're assigning '5' into $a. '5', obviously, has the value 5, or in other words '5' is an expression"

http://php.net/manual/en/language.expressions.php

现在,当您在手册中阅读有关变量作用域的内容时,他们正好有这个示例:

Now when you read about variable scope in the manual, they have exactly this example:

    function test()
    {
    static $a = 0;
    echo $a;
    $a++;
    }

所以,关于变量作用域的手册说 static $a = 0;很好,而关于表达式的手册说 $a = 5,将是一个表达式.这基本上是一样的,只是一个 0 而不是 5 ...

So, the manual about variable scopes says static $a = 0; is fine, while the manual about expressions says $a = 5, would be an expression. Which is basically the same thing, just a 0 instead of a 5...

所以我现在有点困惑.

现在到底什么是表达式,我究竟可以或不能分配给静态变量的是什么?:)

What exactly is an expression now and what exactly can I or can I not assign to static variables? :)

推荐答案

你不能初始化一个 static 变量使用 non-常量表达式.初始化后,您可以分配任何您喜欢的内容.

You cannot initialize a static variable using a non-constant expression. You can assign anything you like to it after it is initialized.

区别在于 static 变量在解析阶段被初始化,即 PHP 通读源代码以找出什么是什么.在那个阶段,没有代码被执行,PHP 只是读取你想要它做的事情.因此,它不会执行代码来初始化变量.

The difference is that static variables are initialized during the parsing phase, i.e. while PHP reads through the source code to figure out what's what. At that stage no code is executed, PHP just reads what you want it to do. Therefore, it will not execute code to initialize the variable.

static $foo = 'bar';

'bar' 是一个 常量值,PHP 可以在解析时轻松地将其分配给变量.

'bar' is a constant value which PHP can easily assign to the variable at parse time.

static $foo = Bar::baz();

Bar::baz() 是一个需要运行的表达式,PHP需要定位类,必要时加载它,运行baz() 方法,它可以做各种不同的事情......对于 5 + 3md5('bar') 或任何需要实际的东西都是一样的计算.PHP 不会在解析时做所有这些动态的事情.因此,除了常量值之外,您不能初始化一个static变量.

Bar::baz() is an expression that needs to run, PHP needs to locate the class, load it if necessary, run the baz() method, which may do all sorts of different stuff... The same for 5 + 3, md5('bar') or anything that requires actual computation. PHP is simply not going to do all this dynamic stuff at parse time. Therefore you cannot initialize a static variable with anything but constant values.

运行时,您可以将您喜欢的任何内容分配给static 变量.一个经常使用的模式是这样的:

At runtime, you can assign anything you like to a static variable. An often used pattern is this:

static $foo = null;
if ($foo === null) {
    $foo = new SomeObject;
}

这将 SomeObject 的实例保存在 static 变量中,但你不能用它来初始化变量.

This keeps the instance of SomeObject in the static variable, but you cannot initialize the variable with it.

这篇关于用 PHP 学习 OO 编码,静态 != 表达式,但 PHP 手册说所有有值的都是表达式,困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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