I cannot pass Methods as Parameter in PHP

Asked

Viewed 87 times

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 correct ?

Att:

On the basis of the answer given by my colleague below, I have decided in this way :

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;
}
  • 1

    You cannot set a function as the default value of a parameter.

  • @rray and what should I do to pass the result to the next method ?

3 answers

2


I believe you have two mistakes first currentYear() will look for a function and not a method within the class:

$current_year = currentYear()

To access the method is required $this

Second, even if I do this:

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

It wouldn’t work because the $this would not be accessible, what I recommend is to use null as standard 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;
}
  • 1

    It’s also an option.

  • 2

    It was the best ! show William.

0

The correct is to use the "variável" in class and not only in 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 wouldn’t have to have an "automatic" Setter, remember that !

This whole class didn’t understand her reason, but doing what you want is there !

  • Gave error: Using $this when not in Object context

  • Put your whole class together for us to see.

  • I posted the whole class @Raoni BZ

  • Yuri, I refilled and tested the code without the extension of the class, and the rest of the code just to see and understand how it would be, because I didn’t understand the need for some methods.

-1

PHP is not a functional language, in this case, vc will not be able to pass a method as parameter, vc 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

  • 1

    The variable $current_year is defined only for the method currentYear(), thus it has no value outside the method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.