关于PHP中的范围和OOP [英] About scope and OOP in PHP

查看:69
本文介绍了关于PHP中的范围和OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何使用对象方面遇到困难.

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;

  1. 如果我将$a_variable初始化为$a_variable,则仅在函数内部可用,对吗?
  2. 如果我将$a_varialbe初始化为$this->a_variable,则仅在第二类中可用,对吗?
  3. 我可以将$a_variable初始化为$first->second->a_variable吗?如果是这样,我怎么称呼它为#1#2#3?
  4. 我可以初始化$a_varialbe as $this->second->a_variable吗?如果是这样,我怎么称呼它为#1#2#3?
  1. If I initialize $a_variable as $a_variable it is only available inside the function, correct?
  2. If I initialize $a_varialbe as $this->a_variable it is only available inside class second, correct?
  3. Can I initialize $a_variable as $first->second->a_variable? If so, How would I call it at #1, #2, and #3?
  4. 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:

  1. 变量具有功能范围
  2. 如果引用了对象,则可以访问
  3. 对象属性
  4. 可以限制对象属性的可见性
  1. variables have function scope
  2. object properties are accessible if you have a reference to the object
  3. 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 $as 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是对象.它在范围内,其属性apublic,因此可以使用$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屋!

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