I can not pass Methods as Parameter in PHP [duplicate]

0

I have the following code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DatesController extends Controller
{
    //Função que calcula o inicio e o fim da semana a partir do dia 
    public static function getWeekBeginEnd($day)
    {
        $day = strtotime($day);
        //Pega o dia da semana
        $number_of_day = date('w', $day);
        intval($number_of_day);
        //Começa a contar a semana na segunda
        $number_of_day--;
        //Clcula o primeiro dia da Semana
        if ($number_of_day < 0) {
            $begin_week = strtotime('-7 days', $day);
        }
        else
            $begin_week = strtotime('-'.$number_of_day.' days', $day);
        // Calcula o ultimo da semana
        $end_week = strtotime('+7 days', $begin_week);
        //Coloca os dados numa array
        $week['begin'] = date('Y-m-d', $begin_week);
        $week['end'] = date('Y-m-d', $end_week);
        //retorna a array
        return $week;
    }

    public function currentYear()
    {
        $current_year = date("Y");
        return $current_year;
    }

    public function currentMonth()
    {
        $current_month = date("m");
        return $current_month;
    }

    public function yearBegin($current_year = currentYear())
    {
        $year_begin = $current_year."-01-01";
        return $year_begin;
    }

    public function yearEnd($current_year = currentYear())
    {
        $year_end = $current_year."-12-31";
        return $year_end;
    }

    public function thisMonthBegin($current_year = currentYear(), $current_month = currentMonth())
    {
        $this_month_begin = $current_year."-".$current_month."-01";
    }

    public function nextMonth($current_month = currentMonth())
    {
        $next_month = $current_month + 1;
        if (strlen($next_month) == 1) {
            $next_month = "0".$next_month;
        }
        return $next_month;
    }

    public function nextMonthBegin($current_year = currentYear(), $next_month = nextMonth())
    {
        $next_month_begin = $current_year."-".$next_month."-01";
        return $next_month_begin;
    }

    public function now()
    {
        $now = date("Y-m-d H:i");
        return $now;
    }

    public function today()
    {
        $today = date("Y-m-d");
        return $today;
    }

    public function todayWithoutSeparators()
    {
        $todaywithoutseparators = date("Ymd");
        return $todaywithoutseparators;
    }

    public function todayTimeStamp($today = today())
    {
        $today_timestamp = strtotime($today);
        return $today_timestamp;
    }

    public function yesterdayTimeStamp($today_timestamp = todayTimeStamp())
    {
        $yesterday_timestamp = strtotime("-1 day", $today_timestamp);
        return $yesterday_timestamp;
    }

    public function yesterday($yesterday_timestamp = yesterdayTimeStamp())
    {
        $yesterday = date("Y-m-d", $yesterday_timestamp);
        return $yesterday;
    }

    public function tomorrowTimestamp($today_timestamp = todayTimeStamp())
    {
        $tomorrow_timestamp = strtotime("+1 day", $today_timestamp);
        return $tomorrow_timestamp;
    }

    public function tomorrow($tomorrow_timestamp = tomorrowTimestamp())
    {
        $tomorrow = date("Y-m-d", $tomorrow_timestamp);
        return $tomorrow;
    }

    public function aftomorrowTimestamp($today_timestamp = todayTimeStamp())
    {
        $aftomorrow_timestamp = strtotime("+2 day", $today_timestamp);
        return $aftomorrow_timestamp;
    }

    public function aftomorrow($aftomorrow_timestamp = aftomorrowTimestamp())
    {
        $aftomorrow = date("Y-m-d", $aftomorrow_timestamp);
        return $aftomorrow;
    }

    public function twoDaysAgoTimestamp($today_timestamp = todayTimeStamp())
    {
        $two_days_ago_timestamp = strtotime("+3 day", $today_timestamp);
        return $two_days_ago_timestamp;
    }

    public function twoDaysAgo($two_days_ago_timestamp = twoDaysAgoTimestamp())
    {
        $two_days_ago = date("Y-m-d", $two_days_ago_timestamp);
        return $two_days_ago;
    }

}

I'm getting the following error in the last method:

Constant expression contains invalid operations

How to fix?

Att:

Based on my colleague's answer below, I resolved this:

public static function currentYear()
{
    $current_year = date("Y");
    return $current_year;
}

public static function currentMonth()
{
    $current_month = date("m");
    return $current_month;
}

public static function yearBegin()
{
    $current_year = $this->currentYear();
    $year_begin = $current_year."-01-01";
    return $year_begin;
}
    
asked by anonymous 09.10.2017 / 17:28

3 answers

2

I think I have two errors, first currentYear() will look for a function and not a method within the class:

$current_year = currentYear()

To access the method you need $this

Second, even if you do this:

... yearBegin($current_year = $this->currentYear()) {

It would not work, as $this would not be accessible, what I recommend is to use null as the default parameter and make a if to check, like this:

public function yearBegin($current_year = null)
{
    $current_year = $current_year ? $current_year : $this->currentYear();

    $year_begin = $current_year."-01-01";
    return $year_begin;
}
    
09.10.2017 / 17:47
0

Correct is to use " variável " in the class, not just the methods. Example:

<?php

class DatesController 
{

    private $year;

    //Função que calcula o inicio e o fim da semana a partir do dia 

    //... restante da classe

    public function setCurrentYear()
    {
        //return $this -> year = date('Y');
       return $this -> year = date('Y');
    }

    public function yearBegin()
    {
        $year_begin = $this->setCurrentYear()."-01-01";
        return $year_begin;
    }

}

$obj = new DatesController();
echo $obj -> yearBegin();

Actually, you do not have to have an "automatic" setter, remember that!

All this class did not understand why, but doing what you want is there!

    
09.10.2017 / 17:38
-1

PHP is not a functional language, in this case, you will not be able to pass a method as a parameter, you can do so.

$current_year = currentYear();//atribui o retorno do método a variável $current_year

yearBegin($current_year);// passa como parametro a variavel $current_year

    
09.10.2017 / 17:36