如何从 CLASS 获取值?

问题描述 投票:0回答:1

showMonth 函数的 $this->_month $this->_year 值不显示。

monthData 函数也不输出。

我该怎么办?

1.php
if(!$year) {  $year=date('Y');  $month=date('n');  $day=date('j'); } 

echo "$year $month $day "; // 2024 5 4


$cal = new Calendar($year,$month);
echo $cal->showMonth($_SESSION['Iyagi_Id']);


2.php

class Calendar
{

    var $_year  = null;
    var $_month = null;
    var $_day   = null;
    
function Calendar($year,$month) {
    
        $this->_year  = $year  ? $year  : date('Y');
        $this->_month = $month ? $month : date('n');
        $this->_day   = date('j');              
    }


function showMonth ()
    {
    
   echo "$this->_month $this->_year cale 88";
   return $this->monthData($this->_month, $this->_year);
    }

function monthData ($setMonth, $setYear) {
        
        GLOBAL $setMonth, $setYear;
                
         echo "-105 $setMonth , $setYear 105-";
}

我正在尝试获取日期,但没有任何价值。

php php-8.3
1个回答
0
投票

从 PHP 8.0.0 开始,与类同名的方法不再被视为构造函数,所以你必须使用魔法名称

__construct
来代替。

© www.soinside.com 2019 - 2024. All rights reserved.