创建php日历类时变量的问题

时间:2012-01-09 22:38:13

标签: php oop

我正在尝试在php中创建一个日历类,并且在返回定义为public的类变量时遇到问题。

以下是我班级的代码

<?php 

class CalendarClass {

// property declaration

public $months = array();
public $year;
public $month;
public $day;
public $fourYear;
public $monthFirst;
public $monthMax;
public $monthVal;

private $data = array();

//My set functions

function __construct() {
    $year = date("y");;
    $month = date("n");;
    $day = date("j");;
    $fourYear = date("Y");
    $this->setDate($year, $month, $day, $fourYear);
    echo $fourYear; // echos 2012
}

//I'm not entirely sure if these 2 are set up correctly
//From what I read these are automatically called when
//you set or get variables.
public function __set($dt, $vl) {
    $this->data[$dt] = $vl;
}

public function __get($dt) {
    return $this->data[$dt];
}


//Sets the date and all other variables accordingly
//Must have in this order
//2 digit year, 1/2 digit month and day (ex: 1, 12), 4 digit year 
public function setDate($y, $m, $d, $fourY){
    $year = $y;
    $month = $m;
    $day = $d;
    $fourYear = $fourY; 

    if($day == -1){
        $month = $month - 1;
        if($month == -1){
            $month = 11;
            $fourYear--;
            $year = substr($fourYear, -2);
        }
    }elseif($day > $this->monthMax){
        $month = $month + 1;
        if($month == 12){
            $month = 11;
            $fourYear++;
            $year = substr($fourYear, -2);
        }
    }

    if($month == -1){
        $month = 11;
        $fourYear--;
        $year = substr($fourYear, -2);
    }elseif($month == 12){
        $month = 11;
        $fourYear++;
        $year = substr($fourYear, -2);
    }

    $monthFirst = $this->valueSet($year, $month, $day, $fourYear);
    $monthMax = $this->monthReturn($month, "Max");
    $monthVal = $this->monthReturn($month, "Value");
}

//Get functions
public function getYear(){
    $test = $this->year;
    echo $test; //echos nothing
    return $this->year; 
}
public function getMonth(){
    return $this->month;    
}
public function getDay(){
    return $this->day;  
}
public function getFourYear(){
    return $this->fourYear; 
}
public function getMonthFirst(){
    return $this->monthFirst;   
}
public function getMonthMax(){
    return $this->monthMax; 
}
public function getMonthVal(){
    return $this->monthVal; 
}

// method declaration

//Returns the year portion of the equation
public function year($currentYear){

    //calculating year value
    $yearTotal = 0;
    if($currentYear > 84){
        $yearTotal = $currentYear - 84;
    }elseif($currentYear > 56){
        $yearTotal = $currentYear - 56;
    }elseif($currentYear > 28){
        $yearTotal = $currentYear - 28;
    }else{
        $yearTotal = $currentYear;
    }
    $yearTotal = $yearTotal + floor($currentYear / 4);
    while($yearTotal > 7){
        $yearTotal = $yearTotal - 7;
    }
    return $yearTotal;
}

//Returns the month portion of the equation
public function month($currentMonth, $four_Year){
    //Calculating month value

    //$this->month;
    $monthTotal = $this->monthReturn($currentMonth, "Value");
    if((($four_Year % 4) == 0) && ($currentMonth == 0 || $currentMonth == 1)){

            $monthTotal = $monthTotal - 1;
    }
    return $monthTotal;
}

//Returns the day portion of equation
public function day($currentDay){

    //Calculating day value
    $dayTotal = $currentDay;
    while($dayTotal > 7){
        $dayTotal = $dayTotal - 7;  
    }
    return $dayTotal;
}

//Return the total value for equation(weekday 
//"0" for sunday 
//"1" for monday 
//"2" for tuesday
public function total($yearTotal, $monthTotal, $dayTotal){

    $total = $yearTotal + $monthTotal + $dayTotal;

    $fourYear = date('Y');

    if($fourYear > 1699 && $fourYear < 1800){
        $total = $total + 5;
    }
    if($fourYear > 1799 && $fourYear < 1900){
        $total = $total + 3;    
    }
    if($fourYear > 1899 && $fourYear < 2000){
        $total = $total + 1;
    }
    if($fourYear > 1999 && $fourYear < 2100){
        $total = $total + 0;
    }
    if($fourYear > 2099 && $fourYear < 2200){
        $total = $total + 2;
    }
    if($fourYear > 2099 && $fourYear < 2200){
        $total = $total + 4;
    }


    while($total > 7){
        $total = $total - 7;
    }
    return $total;
}   

//Return the Maximum amount of day in the month
//or return the month value for equation
//2nd variable should be either 
//"Max" - for maximum amount of days in month or 
//"Value" - for the month value
public function monthReturn($month, $action){
    global $months;
    $m = $month - 1;
    if($action == "Max"){
        $max = $months[$m][0];
        return $max;
    }elseif($action == "Value"){
        $value = $months[$m][1];
        return $value;
    }else{
        return -1;  
    }

}

//Get the weekday of a certain day
public function valueSet($currentYear, $currentMonth, $currentDay, $four_Year){

$y = $this->year($currentYear);
//echo $y . "<br />";
$m = $this->month($currentMonth, $four_Year);
//echo $m . "<br />";
$d = $this->day($currentDay);
//echo $d . "<br />";
$weekday = $this->total($y, $m, $d);
return $weekday;

}
    //End of class  
    }



    ?>

这是使用该类的代码         

    include_once("f_calendar.php"); 

    $thisMonth = new CalendarClass;

    $nextMonth = new CalendarClass;

    $prevMonth = new CalendarClass;

    $y = $thisMonth->getYear();

    $m = $thisMonth->getMonth();

    $d = $thisMonth->getDay();

    $fY = $thisMonth->getFourYear();

    $thisMonth->setDate($y, $m, $d, $fY);

    $nextMonth->setDate($y, $m + 1, "1", $fY);

    $prevMonth->setDate($y, $m - 1, "1", $fY);

    echo $thisMonth->fourYear; //echoes nothing

    $weekDay = $thisMonth->getMonthFirst();

    $testYear = $thisMonth->getYear();

    $i = $prevMonth->getMonthMax() - $weekDay;

    $totalDays = $weekDay + $thisMonth->getMonthMax();

    $totalWeeks = ceil($totalDays / 7);

    $days = $totalWeeks * 7;

    $prevMonthMax = $prevMonth->getMonthMax();

    $thisMonthMax = $thisMonth->getMonthMax();

    $nextMonthMax = $nextMonth->getMonthMax();

    ?>

正在定义对象,因为标志回波从它们内部输出到屏幕。我原本以为它可以使用我自己的get函数但是当它不起作用时我开始更多地了解它并学习了魔术函数__set__get但是我还没有完成那些。我是否必须重做我的大部分代码以适应魔术功能?或者有更简单,更容易的方法吗?特别是使用我当前的代码而没有打算重复使用此代码,所以我想让它100%正确,所以我永远不必重做它。

由于 安德鲁

1 个答案:

答案 0 :(得分:0)

您的问题是,您从未将值分配给该类的对象属性“fourYear”。 你这样做

$fourYear = 'new value';

你必须这样做:

$this->fourYear = 'new value';

请不要使用魔术函数__get和__set。你在这里不需要它们。 你需要它们,如果你想在存储它之前对它做一些事情。

这是你的更新和工人阶级:

<?php 

class CalendarClass {

// property declaration

public $months = array();
public $year;
public $month;
public $day;
public $fourYear;
public $monthFirst;
public $monthMax;
public $monthVal;

private $data = array();
//My set functions

function __construct() {
    $year = date("y");;
    $month = date("n");;
    $day = date("j");;
    $this->fourYear = date("Y");
    $this->setDate($year, $month, $day, $this->fourYear);
    echo $this->fourYear; // echos 2012
}

//I'm not entirely sure if these 2 are set up correctly
//From what I read these are automatically called when
//you set or get variables.


//Sets the date and all other variables accordingly
//Must have in this order
//2 digit year, 1/2 digit month and day (ex: 1, 12), 4 digit year 
public function setDate($y, $m, $d, $fourY){
    $year = $y;
    $month = $m;
    $day = $d;
    $this->fourYear = $fourY; 

    if($day == -1){
        $month = $month - 1;
        if($month == -1){
            $month = 11;
            $this->fourYear--;
            $year = substr($this->fourYear, -2);
        }
    }elseif($day > $this->monthMax){
        $month = $month + 1;
        if($month == 12){
            $month = 11;
            $this->fourYear++;
            $year = substr($this->fourYear, -2);
        }
    }

    if($month == -1){
        $month = 11;
        $this->fourYear--;
        $year = substr($this->fourYear, -2);
    }elseif($month == 12){
        $month = 11;
        $this->fourYear++;
        $year = substr($this->fourYear, -2);
    }

    $monthFirst = $this->valueSet($year, $month, $day, $this->fourYear);
    $monthMax = $this->monthReturn($month, "Max");
    $monthVal = $this->monthReturn($month, "Value");
}

//Get functions
public function getYear(){
    $test = $this->year;
    echo $test; //echos nothing
    return $this->year; 
}
public function getMonth(){
    return $this->month;    
}
public function getDay(){
    return $this->day;  
}
public function getFourYear(){
    return $this->fourYear; 
}
public function getMonthFirst(){
    return $this->monthFirst;   
}
public function getMonthMax(){
    return $this->monthMax; 
}
public function getMonthVal(){
    return $this->monthVal; 
}

// method declaration

//Returns the year portion of the equation
public function year($currentYear){

    //calculating year value
    $yearTotal = 0;
    if($currentYear > 84){
        $yearTotal = $currentYear - 84;
    }elseif($currentYear > 56){
        $yearTotal = $currentYear - 56;
    }elseif($currentYear > 28){
        $yearTotal = $currentYear - 28;
    }else{
        $yearTotal = $currentYear;
    }
    $yearTotal = $yearTotal + floor($currentYear / 4);
    while($yearTotal > 7){
        $yearTotal = $yearTotal - 7;
    }
    return $yearTotal;
}

//Returns the month portion of the equation
public function month($currentMonth, $four_Year){
    //Calculating month value

    //$this->month;
    $monthTotal = $this->monthReturn($currentMonth, "Value");
    if((($four_Year % 4) == 0) && ($currentMonth == 0 || $currentMonth == 1)){

            $monthTotal = $monthTotal - 1;
    }
    return $monthTotal;
}

//Returns the day portion of equation
public function day($currentDay){

    //Calculating day value
    $dayTotal = $currentDay;
    while($dayTotal > 7){
        $dayTotal = $dayTotal - 7;  
    }
    return $dayTotal;
}

//Return the total value for equation(weekday 
//"0" for sunday 
//"1" for monday 
//"2" for tuesday
public function total($yearTotal, $monthTotal, $dayTotal){

    $total = $yearTotal + $monthTotal + $dayTotal;

    $this->fourYear = date('Y');

    if($this->fourYear > 1699 && $this->fourYear < 1800){
        $total = $total + 5;
    }
    if($this->fourYear > 1799 && $this->fourYear < 1900){
        $total = $total + 3;    
    }
    if($this->fourYear > 1899 && $this->fourYear < 2000){
        $total = $total + 1;
    }
    if($this->fourYear > 1999 && $this->fourYear < 2100){
        $total = $total + 0;
    }
    if($this->fourYear > 2099 && $this->fourYear < 2200){
        $total = $total + 2;
    }
    if($this->fourYear > 2099 && $this->fourYear < 2200){
        $total = $total + 4;
    }


    while($total > 7){
        $total = $total - 7;
    }
    return $total;
}   

//Return the Maximum amount of day in the month
//or return the month value for equation
//2nd variable should be either 
//"Max" - for maximum amount of days in month or 
//"Value" - for the month value
public function monthReturn($month, $action){
    global $months;
    $m = $month - 1;
    if($action == "Max"){
        $max = $months[$m][0];
        return $max;
    }elseif($action == "Value"){
        $value = $months[$m][1];
        return $value;
    }else{
        return -1;  
    }

}

//Get the weekday of a certain day
public function valueSet($currentYear, $currentMonth, $currentDay, $four_Year){

$y = $this->year($currentYear);
//echo $y . "<br />";
$m = $this->month($currentMonth, $four_Year);
//echo $m . "<br />";
$d = $this->day($currentDay);
//echo $d . "<br />";
$weekday = $this->total($y, $m, $d);
return $weekday;

}
    //End of class  
    }



?>