关于PHP中的范围和OOP [英] About scope and OOP in PHP
问题描述
我在理解如何使用对象方面遇到困难.
I am having trouble understanding how to work with objects.
具体代码:
class first{
class second{
public function widgets(){
$a_variable = $a_value;
}
#1
}
$second = new second;
#2
}
#3
$first = new first;
- 如果我将
$a_variable
初始化为$a_variable
,则仅在函数内部可用,对吗? - 如果我将
$a_varialbe
初始化为$this->a_variable
,则仅在第二类中可用,对吗? - 我可以将
$a_variable
初始化为$first->second->a_variable
吗?如果是这样,我怎么称呼它为#1
,#2
和#3
? - 我可以初始化
$a_varialbe as $this->second->a_variable
吗?如果是这样,我怎么称呼它为#1
,#2
和#3
?
- If I initialize
$a_variable
as$a_variable
it is only available inside the function, correct? - If I initialize
$a_varialbe
as$this->a_variable
it is only available inside class second, correct? - Can I initialize
$a_variable
as$first->second->a_variable
? If so, How would I call it at#1
,#2
, and#3
? - Can I initialize
$a_varialbe as $this->second->a_variable
? If so, How would I call it at#1
,#2
, and#3
?
如您所见,我对OOP的工作原理感到困惑.
As you can see I am simply confused as to how OOP works.
首先,我想表达我对所有帮助的感谢.我已经学到了足够多的知识,可以认为这个问题取得了巨大成功.
First of all, I want to express how much I appreciate all of the help. I have already learned more than enough to consider this question a smashing success.
也就是说,即使编写不当,伪代码和无效的语法,该代码也可以运行.
That said, even if it is poorly formulated, psuedo-code and invalid syntax, this code DOES run.
class class_1{
public function function_1(){
require('class_2.php');
public function function_2_callback(){
//%%%%%% How do I set a variable here and get the DATA...
}
$this->class_2 = new class_2("function_2_callback");
}
}
$class_1 = new class_1;
//&&&&&&&&&& Get the DATA here?
/* CONTENTS OF class_2.php */
class class_2($callback){
call_user_function($callback);
}
即使我们必须将此视为练习.有人可以告诉我如何首先设置(@ %%%%%%%%),然后如图所示调用变量(@&&&&&& amp;)吗?
Even if we have to look at this as an exercise. Can someone tell me how I would first set (@ %%%%%%%)and then call a variable (@ &&&&&&&&) as shown?
推荐答案
首先:您所拥有的东西行不通,您无法按照自己的方式在类内声明类(尽管有条件地在a内声明类)功能,而您不应该这样做.)
First off: What you have there doesn't work, you cannot declare a class inside a class the way you are doing (notwithstanding conditionally declaring a class inside a function, which you should not do).
PHP(包括OOP)的作用域非常简单:
Scope in PHP (including OOP) is very simple:
- 变量具有功能范围 如果引用了对象,则可以访问
- 对象属性
- 可以限制对象属性的可见性
- variables have function scope
- object properties are accessible if you have a reference to the object
- the visibility of object properties can be restricted
您唯一真正的作用域是变量的函数作用域:
The only real scope you have is function scope for variables:
$a = 'foo';
function bar() {
$a = 'bar';
}
这两个$a
完全无关,范围不同.就这么简单.
The two $a
s are entirely unrelated, in different scopes. As simple as that.
class Foo {
public $a = 'foo';
public function bar() {
$this->a; // foo
$a = 'bar';
}
}
$foo = new Foo;
$foo->a; // foo
一个对象属性没有作用域,它具有可见性.如果范围内的对象,则可以访问它.在上面,$foo
是对象.它在范围内,其属性a
为public
,因此可以使用$foo->a
进行访问.在类内部,可以通过$this->a
访问该属性.
An object property has no scope, it has visibility. You can access it if you have the object in scope. Above, $foo
is the object. It's in scope, its property a
is public
, therefore it can be accessed using $foo->a
. Inside the class, the property is accessible via $this->a
.
$a = 'bar'
是函数中的局部变量,与$this->a
无关.除了函数内部以外,其他任何地方都无法访问它.请参阅规则1,功能范围.
The $a = 'bar'
is a local variable in the function and has nothing to do with $this->a
. It is not accessible anywhere except inside the function. Refer to rule #1, function scope.
class Bar {
protected $b = 'bar';
public function baz() {
$this->b; // bar
}
}
$bar = new Bar;
$bar->b; // doesn't work
如果 visibility 不是public
,则无法从类本身之外访问该属性(此处为b
).在类内部,您可以使用$this->b
进行访问,但不能使用$bar->b
从外部进行访问.与 scope 无关,而是与可见性有关.
If the visibility is not public
, the property (here b
) is not accessible from outside the class itself. Inside the class you can access it using $this->b
, but not from outside using $bar->b
. It's not about scope, but visibility.
这几乎就是PHP中的范围规则.
And that's pretty much the scope rules in PHP.
这篇关于关于PHP中的范围和OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!