如何在 PHP 中的静态函数中访问私有成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1957629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:26:14  来源:igfitidea点击:

How to access a private member inside a static function in PHP

phpclassstaticprivatemember

提问by VolkerK

I have the following class in PHP

我在 PHP 中有以下类

class MyClass
{
  // How to declare MyMember here? It needs to be private
  public static function MyFunction()
  {
    // How to access MyMember here?
  }
}

I am totally confused about which syntax to use

我对使用哪种语法完全感到困惑

$MyMember = 0;and echo $MyMember

$MyMember = 0;echo $MyMember

or

或者

private $MyMember = 0;and echo $MyMember

private $MyMember = 0;echo $MyMember

or

或者

$this->MyMember = 0;and echo $this->MyMember

$this->MyMember = 0;echo $this->MyMember

Can someone tell me how to do it?

有人可以告诉我该怎么做吗?

I am kind of not strong in OOPS.

我不太擅长 OOPS。

Can you do it in the first place?

你能做到吗?

If not, how should I declare the member so that I can access it inside static functions?

如果没有,我应该如何声明成员以便我可以在静态函数中访问它?

回答by VolkerK

class MyClass
{
  private static $MyMember = 99;

  public static function MyFunction()
  {
    echo self::$MyMember;
  }
}

MyClass::MyFunction();

see Visibilityand Scope Resolution Operator (::)in the oop5 chapter of the php manual.

请参阅php 手册 oop5 章节中的Visibilityand Scope Resolution Operator (::)

回答by Michael

This is a super late response but it may help someone..

这是一个超级迟到的回应,但它可能会帮助某人..

class MyClass
{
  private $MyMember;

  public static function MyFunction($class)
  {
    $class->MyMember = 0;
  }
}

That works. You can access the private member that way, but if you had $class you should just make MyFunction a method of the class, as you would just call $class->MyFunction(). However you could have a static array that each instance is added to in the class constructor which this static function could access and iterate through, updating all the instances. ie..

那个有效。您可以通过这种方式访问​​私有成员,但是如果您有 $class,您应该只将 MyFunction 设为类的方法,就像您只需调用 $class->MyFunction() 一样。但是,您可以将每个实例添加到类构造函数中的静态数组,该静态函数可以访问和迭代,更新所有实例。IE..

class MyClass
{
  private $MyMember;
  private static $MyClasses;

  public function __construct()
  {
    MyClass::$MyClasses[] = $this;
  }

  public static function MyFunction()
  {
    foreach(MyClass::$MyClasses as $class)
    {
      $class->MyMember = 0;
    }
  }
}

回答by Arno

Within static methods, you can't call variable using $thisbecause static methods are called outside an "instance context".

在静态方法中,您不能调用变量 using,$this因为静态方法是在“实例上下文”之外调用的。

It is clearly stated in the PHP doc.

PHP 文档中明确说明了这一点。

回答by miku

<?php
    class MyClass
    {
        // A)
        // private $MyMember = 0;

        // B)
        private static $MyMember = 0;

        public static function MyFunction()
        {
            // using A) //  Fatal error: Access to undeclared static property: 
            //              MyClass::$MyMember
            // echo MyClass::$MyMember; 

            // using A) // Fatal error: Using $this when not in object context
            // echo $this->MyMember; 

            // using A) or B)
            // echo $MyMember; // local scope

            // correct, B) 
            echo MyClass::$MyMember;
        }
    }

    $m = new MyClass;
    echo $m->MyFunction();
    // or better ...
    MyClass::MyFunction(); 

?>

回答by DevWL

Static or non-static?

静态还是非静态?

Did you ever asked yourself this question?

你有没有问过自己这个问题?

You can not access non static parameters / methods from inside static method (at least not without using dependency injection)

You can however access static properties and methods from with in non-static method (with self::)

您不能从静态方法内部访问非静态参数/方法(至少在不使用依赖注入的情况下不能)

但是,您可以从非静态方法中的 with 访问静态属性和方法(with self::

Properties

特性

Does particular property value is assign to class blueprint or rather to it instance (created object from a class)? If the value is not tight to class instance (class object) then you could declare it as as static property.

特定的属性值是分配给类蓝图还是分配给它的实例(从类创建的对象)?如果该值与类实例(类对象)不紧密,那么您可以将其声明为静态属性

private static $objectCreatedCount; // this property is assign to class blueprint
private $objectId; // this property is assign explicitly to class instance

Methods

方法

When deciding on making a method static or non-static you need to ask yourself a simple question. Does this method need to use $this? If it does, then it should not be declared as static.

在决定将方法设为静态还是非静态时,您需要问自己一个简单的问题。这个方法需要用$this吗?如果是,则不应将其声明为 static

And just because you don't need the $this keyword does not automatically mean that you should make something static (though the opposite is true: if you need $this, make it non-static).

并且仅仅因为您不需要 $this 关键字并不自动意味着您应该将某些东西设为静态(尽管相反是正确的:如果您需要 $this,请将其设为非静态)。

Are you calling this method on one individual object or on the class in general? If you not sure which one to use because both are appropriate for particular use case, then always use non-static. It will give you more flexibility in future.

您是在单个对象上还是在整个类上调用此方法?如果您不确定使用哪一个,因为两者都适合特定用例,那么请始终使用非静态。它将为您提供更多的灵活性。

Good practice is to always start to design your class as non-static and force static if particular us case become very clear.

好的做法是始终开始将您的类设计为非静态并在特定用例变得非常清楚时强制使用静态。

You could try to declare your parameters as static... just so you can access it from static method but that usually is not what you want to do. So if you really need to access $thisfrom static method then it means that you need to rethink/redesign your class architecture because you have don it wrong.

您可以尝试将您的参数声明为静态...这样您就可以从静态方法访问它,但这通常不是您想要做的。因此,如果您真的需要$this从静态方法访问,那么这意味着您需要重新思考/重新设计您的类架构,因为您做错了。