<?php
//===============================================================================================
// define a Planet class
//===============================================================================================
class Planet
{
    public  static $className    = 'Our Solar System';          //public  class property
    public  static $totalPlanets = 8;                           //public class property
    private static $countPlanets = 0;                           //private class property

    private $planetName;                                        //object properties
    private $diameter;
    private $distance;
    public  $year;
    public  $day;
    
    function __construct($name,$length,$distance,$year,$day)      //constructor 
    {
        $this->planetName = $name;                                //assign incoming param
        $this->diameter   = $length;                              //to object properties
        $this->distance   = $distance;
        $this->year       = $year;
        $this->day        = $day;
         
        static::$countPlanets += 1;         
    }    

    function __get($prop_name)                  		//will automatically be used if property is private
    {
        return($this->$prop_name);              		//return the value of the property                                      
    }
    function __set($prop_name, $value)          		//will automatically be used if property is private
    {
        if($prop_name == 'planetName')                  //if the property name is 'planetName'
        {
            $validList = array('Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune');

            if (! in_array($value, $validList))         //validate it against the above list
            {
                print "Invalid Planet Name";
                return;
            }
        }
        $this->$prop_name = $value;             		//set the value of the property
    }   


    function __toString()               //will automatically be called when obj is used in string context 
    {
        $volume = $this->computeVolume();
        $volume = number_format($volume,0);

        $str  = "<TABLE BGCOLOR=dddddd BORDER=2 WIDTH=400 ALIGN=CENTER>"
              . "<caption>"                                 . static::$className ." -- "
              . "Planetary data for: <b>"                   . $this->planetName  ."</b></caption>"
              . "<TR><TH>Diameter:<TD>"                     . $this->diameter    ."</TD>"
              . "<TR><TH>Distance from Sun:<TD>"            . $this->distance    ."</TD>"
              . "<TR><TH>One Orbit Around Sun:<TD>"         . $this->year        ."</TD>"
              . "<TR><TH>One Revolution (Earth Time):<TD>"  . $this->day         ."</TD>"
              . "<TR><TH>Planet Volume in Cubic Miles<TD>"  . $volume            ."</TD>"
              . "</TABLE>";

        return ($str);
    }

    static function getCountPlanets()                       //static getter method         
    {
        return (static::$countPlanets);
    }
    static function setCountPlanets($num)                   //static setter method         
    {
        static::$countPlanets = $num;
    }

    static function infoSolarSystem()                       //static method         
    {
        $planetaryName = static::$className;  
        $planetCount   = static::$totalPlanets; 
        $str = "$planetaryName has $planetCount planets";

        return ($str);
    }

    private function computeVolume()                            //instance method               
    {
        $radius = ($this->diameter) /2;  
        $volume = pow($radius,3) * pi() * 4/3; 

        return ($volume);
    }
}
?>