在构造函数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();
?>
dongyan7851
dongyan7851 解决了..非常感谢你
11 个月之前 回复
dongyu4455
dongyu4455 尝试:所有::$Courses[]=$this;。你也可以这样做:array_push(All::$Courses,$this);但如果你问我,那就不那么可读了。这是关于static关键字的手册
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问