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

时间:2022-02-07 22:36:54

I'm doing a project for school and i am using laravel framework for the first time . I'm having a small issue and i've been stuck for several days and tried lots of different ways - nothing worked.

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

I have built a time function that will take several arguments then check the db through while loops then add all the results inside a 'global' array , then another function will test that global array and check for the values inside.

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

The problem i'm having is that i can't get the functions to access the global array properly :

我遇到的问题是我无法正常访问全局数组的函数:

I tried a lot of different ideas online but can;t get the inner functions of the class to access the global array -

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

Does anyone know a simple way how to do it ? Thanks

有谁知道怎么做的简单方法?谢谢

Tried ( at the very top - before the class , and also inside the class at the top )

试过(在最顶层 - 课前,也在课堂顶部)

$Global['ScheduleTest'] = array();

global $ScheduleCheck = array();

(inside class ) private $ScheduleCheck = array();

FULL CODE :::::::

完整代码:::::::

<?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']." ");
            }
        }
    }

?>

1 个解决方案

#1


1  

These "functions" should be defined as methods on a class.

这些“函数”应该被定义为类的方法。

class ScheduleChecker {

    protected $scheduleCheck = array();

    // Your functions should be placed in here!

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

}

Then you can reference the property from inside your methods.

然后,您可以从方法中引用该属性。

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']." ");
        }
    }
}

You'll probably want to bind this to Laravel's container (in app/start/global.php):

您可能希望将其绑定到Laravel的容器(在app / start / global.php中):

App::instance('schedule', new ScheduleChecker);

Then, in your controller, to get the $scheduleCheck property:

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

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

#1


1  

These "functions" should be defined as methods on a class.

这些“函数”应该被定义为类的方法。

class ScheduleChecker {

    protected $scheduleCheck = array();

    // Your functions should be placed in here!

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

}

Then you can reference the property from inside your methods.

然后,您可以从方法中引用该属性。

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']." ");
        }
    }
}

You'll probably want to bind this to Laravel's container (in app/start/global.php):

您可能希望将其绑定到Laravel的容器(在app / start / global.php中):

App::instance('schedule', new ScheduleChecker);

Then, in your controller, to get the $scheduleCheck property:

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

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