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

问题描述

我正在学习OO和课程,我有一些关于OO和PHP课程的问题。

I am learning about OO and classes, I have a couple of questions about OO and classes in PHP


  1. 理解它,扩展另一个类的类简单地意味着扩展另一个类的类可以访问变量/属性和它所扩展的类的函数/方法。这是正确的吗?

  1. As I understand it, a class that extends another class simply means that the class that extends the other class has access to the variables/properties and the functions/methods of the class that it is extending from. Is this correct?

我知道一个静态方法或属性基本上与一个类的过程函数或变量相同,任何地方。这是正确的吗?

I know that a static method or property are basically the same as a procedural function or variable outside of a class and can be used pretty much anywhere. Is this correct?

公共表示任何类可以访问它,私有表示只有被封装的类或从类的所有者可以访问和使用。这是正确的吗?

Public means any class can access it and Private means only the class that is is encapsulated in or a class that is extended from the owner of can access and use. Is this correct?


推荐答案

子类继承其父类的任何 protected public 属性和方法。任何声明 private 的东西都不能使用。

1) Yes, that's correct. A child class inherits any protected or public properties and methods of its parent. Anything declared private can not be used.

2)这是真的。只要类被加载(这与以前的自动加载问题很好),您可以通过范围解析运算符(::)访问静态方法,如下所示: ClassName :: methodName();

2) This is true. As long as the class is loaded (this goes well with your autoloading question from before), you can access static methods via the scope resolution operator (::), like this: ClassName::methodName();

3)你有 public 的含义, , private 方法只能由它们声明的类使用。

3) You have the meaning of public correct, but as I mentioned earlier, private methods can only be used by the class they are declared in.

class parentClass
{
     private $x;
     public $y;
}

class childClass extends parentClass
{
    public function __construct() {
        echo $this->x;
    }
}

$z = new childClass();

上述代码将导致 NOTICE 错误被触发,因为$ x不能被childClass访问。

The above code will result in a NOTICE error being triggered, as $x is not accessible by childClass.

如果$ x被声明为 protected childClass 将具有访问权限。编辑:声明为 protected 的属性可以由声明它的类和扩展它的任何子类访问,否则不能访问外部世界。它是 public private 之间的很好的中间。

If $x was declared protected instead, then childClass would have access. A property declared as protected is accessible by the class that declares it and by any child classes that extend it, but not to the "outside world" otherwise. It's a nice intermediate between public and private.

这篇关于几个关于OO和PHP的PHP的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:27