本文介绍了从php中的静态函数访问私有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级有一个私有变量

I've got a private variable in my class

private $noms = array(
        "HANNY",
        "SYS",
        "NALINE"
);

我想从静态方法访问它:

I want to access it from a static method:

public static function howManyNom($searchValue){

        $ar = $this->noms;

        foreach($ar as $key => $value) {

...

但通常我不能用 $this 检索它,因为静态方法上没有实例.

But as normal I cant retrieve it with $this because there's no instance on a static method.

在我的静态函数中获取 $noms 的正确语法是什么?

What's the right syntax to get $noms inside my static function?

推荐答案

也要将此属性设为静态!

Make this attribute static too!

private static $noms = array(
    "HANNY",
    "SYS",
    "NALINE"
);


public static function howManyNom($searchValue){

    $ar = self::$noms;

    foreach($ar as $key => $value) {

这篇关于从php中的静态函数访问私有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:46