Метод класса объекта выдает фатальную ошибку: допустимый объем памяти 134217728 байт исчерпан (попытался выделить 134217736 байт) - PullRequest
0 голосов
/ 09 апреля 2020

Я запускаю php и получаю фатальную ошибку. Неустранимая ошибка: допустимый объем памяти 134217728 байт исчерпан (попытка выделить 134217736 байт.

Ошибка указывает на код внутри функции addPeriod ( );

Любая помощь приветствуется.

Это код моего класса

class Teacher
{
    public $LName; 
    public $MaxLoad;
    public $TeacherPeriods; 

    function __construct($LName, $MaxLoad, $TeacherPeriods)
    {
        $this->LName=$LName; 
        $this->MaxLoad= $MaxLoad; 
        $this->TeacherPeriods=array(); 
    }

    function addPeriod($period)
    {
        //this is the line the error points me to 
        array_push($this->TeacherPeriods, $period); 
    }
}

Здесь я вызываю метод addPeriod (). Смотрите комментарии // ЗДЕСЬ

    function leastOccurence ($rarray, $n)
{
    $i = 0; 
    $arr=array(); 
    while($i < $n)
    {
        //$arr[$i] = $rarray[$i]['CN']; 
        $i++; 
    }
    // Sort the array same numbers will be side by side
    sort($arr);  
    sort($arr , $n); 

    // find the min frequency  
    // using linear traversal 
    $min_count = $n + 1;  
    $res = -1; 
    $curr_count = 1; 
    for($i = 1; $i < $n; $i++) 
    { 
        if ($arr[$i] == $arr[$i - 1]) 
            $curr_count++; 
        else 
        { 
            if ($curr_count < $min_count) 
            { 
                $min_count = $curr_count; 
                $res = $arr[$i - 1]; 
            } 
            $curr_count = 1; 
        } 
    } 

    // If last element is  
    // least frequent 
    if ($curr_count < $min_count) 
    { 
        $min_count = $curr_count; 
        $res = $arr[$n - 1]; 
    } 
    //gets the index of the first occurance of that Course Number
    return $i; 
} 

/*************************************************************/
/* This function checks if the teacher object already exists */         
/*************************************************************/

function teacherExists($teacherObjArray, $teacherName) {
    foreach ($teacherObjArray as $key => $Teacher) {
        if ($Teacher->LName == $teacherName) return $key; 
    }
    return false;
}

function maintenance($r, $teacherName, $classPeriod, $cn)
{   
    for($i=0; $i<=sizeOf($r); $i++)
    {
        if(($r[$i]['lname']==$teacherName && $r[$i]['period']==$classPeriod) || $r[$i]['cn']==$cn)
        {
            unset($r[$i]); 
        }
    }
    //this reindexes the array, this is important due to how unset works
    $r = array_values($r);

}

/******************/
/* MAIN CODE HERE */            
/******************/ 

//added due to fatal error: Maximum execution time of 30 s exceeded
ini_set('max_execution_time', 300); //300 seconds = 5 minutes

set_time_limit(300);

//the following two arrays will contain the course and teacher objects as each course to teacher assignments are made

$coursesObjArray = array(); 
$teacherObjArray = array(); 

while(sizeOf($r) > 0) 
{

    //here we are storing the row index of the least occuring course
    $j = leastOccurence($r, sizeOf($r)); 
    $courseNumber = $r[$j]['cn'];
    $courseName = $r[$j]['title'];
    $teacherName = $r[$j]['lname']; 
    $classPeriod = $r[$j]['period']; 
    $teacherMaxLoad= $r[$j]['maxload']; 

    //if TeacherExists returns false, it does not exists, must create teacher object
    if (($key = teacherExists($teacherObjArray, $teacherName)) === false) {

        $T = new Teacher($teachernName, $teacherMaxLoad, $classPeriod); 
        $T->LName = $teacherName;
        $T->MaxLoad = $teacherMaxLoad;
        //HERE
        $T->addPeriod($classPeriod);  
        array_push($teacherObjArray, $T); 
        //create class object

        $C = new Course($courseNumber, $courseName, $teacherName, $classPeriod); 
        $C->CourseNumber=$courseNumber;
        $C->CourseName=$courseName; 
        $C->CourseTeacher=$teacherName;
        $C->CoursePeriod=$classPeriod; 
    }
    //if teacher exists and the amount of periods they are assigned to does not match maxload (meaning they are not maxed out) then UPDATE object 
    else
    {
    //get teacher object where its LName property is equal to $teacherName 
        $T = $teacherObjArray[$key];
        //HERE
        $T->addPeriod($classPeriod);
    }
    maintenance($r, $teacherName, $classPeriod, $CourseNumber); 
}

Вот откуда взялся массив $ r

...