PHP-类中的全局变量-在内部函数中访问

PHP-类中的全局变量-在内部函数中访问,php,arrays,laravel,global-variables,Php,Arrays,Laravel,Global Variables,我正在为学校做一个项目,我第一次使用laravel框架。我遇到了一个小问题,我已经被困了好几天,尝试了很多不同的方法,但都没有效果 我构建了一个时间函数,它将接受几个参数,然后通过while循环检查db,然后将所有结果添加到一个“全局”数组中,然后另一个函数将测试该全局数组并检查其中的值 我遇到的问题是,我无法获得正确访问全局数组的函数: 我在网上尝试了很多不同的想法,但是我可以;t获取类的内部函数以访问全局数组- 有人知道一个简单的方法吗?谢谢 试过(在最顶端-课前,也在课内顶端) 完整代码:

我正在为学校做一个项目,我第一次使用laravel框架。我遇到了一个小问题,我已经被困了好几天,尝试了很多不同的方法,但都没有效果

我构建了一个时间函数,它将接受几个参数,然后通过while循环检查db,然后将所有结果添加到一个“全局”数组中,然后另一个函数将测试该全局数组并检查其中的值

我遇到的问题是,我无法获得正确访问全局数组的函数:

我在网上尝试了很多不同的想法,但是我可以;t获取类的内部函数以访问全局数组-

有人知道一个简单的方法吗?谢谢

试过(在最顶端-课前,也在课内顶端)

完整代码:

<?php

global $ScheduleCheck = array() ;

class CourseRegistrationController extends BaseController {

public function __construct() {
    $this->beforeFilter('csrf', array('on'=>'post'));
}

.....

// Function to test time overlaps

function testTimeOverlap($course ,$regday, $start_time,$end_time)
    {
        $start_time1 = (substr($start_time, 0, 5)) ;
        $end_time1 = (substr($end_time, 0, 5)) ;

        $ScheduleArr = makeSchedule();

        $reg_days = explode(",",$regday);

        foreach ($reg_days as $rday)
        {
            foreach ($ScheduleArr as $schedule)
            {

                if((strtolower($rday))==(strtolower($schedule['day'])))
                {

                    $start_time2 = (substr($schedule['stime'], 0, 5)) ;
                    $end_time2 = (substr($schedule['etime'], 0, 5)) ;

                    if(testTime($start_time1,$end_time1,$start_time2,$end_time2))
                    {
                        array_push($ScheduleCheck, array("course"=>$course,"value"=>"true","day"=>$rday ));
                    }
                  else
                  {
                    array_push($ScheduleCheck, array("course"=>$course,"value"=>"false","day"=>$rday ));
                  }

                }
                else
                {
                    array_push($ScheduleCheck, array("course"=>$course,"value"=>"true","day"=>$rday ));
                }

            }

        }
    }


// Another function to go through the global array

function finalTimeTest()
    {
        testNewTime((strtolower(Input::get('course_id'))),(strtolower(Input::get('lecture_id'))),(strtolower(Input::get('tutorial_id'))),(strtolower(Input::get('lab_id'))));

        foreach($ScheduleCheck as $ckTime)
        {
            if($ckTime['value']=="true")
            {
                return true;
            }
            else
            {
                return ($ckTime['course']." ");
            }
        }
    }

?>

这些“函数”应定义为类上的方法

class ScheduleChecker {

    protected $scheduleCheck = array();

    // Your functions should be placed in here!

    public function getScheduleCheck()
    {
        return $this->scheduleCheck;
    }

}
然后可以从方法内部引用该属性

public function finalTimeTest()
{
    // Using $this to call the testNewTime method.
    $this->testNewTime((strtolower(Input::get('course_id'))),(strtolower(Input::get('lecture_id'))),(strtolower(Input::get('tutorial_id'))),(strtolower(Input::get('lab_id'))));

    // Using $this to get the scheduleCheck property.
    foreach($this->scheduleCheck as $ckTime)
    {
        if($ckTime['value']=="true")
        {
            return true;
        }
        else
        {
            return ($ckTime['course']." ");
        }
    }
}
您可能希望将其绑定到Laravel的容器(位于
app/start/global.php
):

然后,在控制器中,要获取
$scheduleCheck
属性:

$scheduleCheck = App::make('schedule')->getScheduleCheck();

仅供参考:通过引入全局关键字,您打破了
OOP
的概念。尝试了其他方法,如在顶部设置私有变量等…不工作不要使用全局变量,相反,您可以将成员
设置为公共
,以便在类外也可以访问它们。尝试了公共静态$ScheduleCheck;-不太管用
App::instance('schedule', new ScheduleChecker);
$scheduleCheck = App::make('schedule')->getScheduleCheck();