donglei2022 2019-02-18 12:27
浏览 72

在构造函数php上推送另一个类的数组属性上的元素

Hello everyone and congrats for your great job, My problem is this: I want to push an object from constructor of a class(Course) to an array property(Courses) of another class(All), without instantiating "All" class.

I tried to do something like this All::array_push($Courses,$this)

Course class

<?php
class Course
{
    public $Title;
    public $Stream;
    public $Type;
    public $StartDate;
    public $EndDate;


    function __construct($title,$stream,$type,$startDate,$endDate)
    {
        $this->Title = $title;
        $this->Stream = $stream;
        $this->Type = $type;
        $this->StartDate = $startDate;
        $this->EndDate = $endDate;
        //This is C# way 
        All.Courses.Add(this);
        //I tried to do something like this 
        //All::array_push($Courses,$this) but not worked
    }
}
?>

All class

<?php

class All
{
    public static $Students = array();
    public static $Assignments = array();
    public static $Trainers = array();
    public static $Courses = array();
    public static $AllCoursesWithTheirStudents = array();
    public static $AllCoursesWithTheirTrainers = array();
    public static $AllCoursesWithTheirAssignments = array();
    public static $AllStudentsWithTheirAssignments = array();
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?