Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 views
in Technique[技术] by (71.8m points)

global - PHP - How do I access values from other functions inside a class without using GLOABL?

I am creating this PHP class, I am having trouble accessing values that have been modified by functions inside my class. When I try to execute the load_widgets() function it returns NULL, but when I try a var_dump() in the store_widget() function, it shows the array I need. Do I have to use global in this case? How would I fix this problem without using global if possible?

<?php

namespace StuffWidgets;

function fetch_widgets(){
    $widgets_include = require CUSTOM_PATH . 'Widgets.php';
    return $widgets_include;
}

class Widgets {

    public static $widget_array_default;
    public static $widget_array_modified;

    public static function store_widget($array){
        self::$widget_array = fetch_widgets();
        array_push(self::$widget_array, $array);
        self::$widget_array_modified = self::$widget_array_default;
        die(var_dump(self::$widget_array)); //Returns the values I need.
    }

    public static function load_widgets(){
        die(var_dump(self::$widget_array_modified)); // Returns NULL, still returns NULL if I use self::$widget_array_default
    }

}

?>
question from:https://stackoverflow.com/questions/65836937/php-how-do-i-access-values-from-other-functions-inside-a-class-without-using-g

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...