Storm Spirit
Storm Spirit

Reputation: 1520

Cannot use isset() on the result of a function call - Laravel

I have a custom helper and a config, I can get the value but when I try the isset() it's not working and it shows me like this

enter image description here

and this is the content of customer helper: app\helper\LmsHelper.php

class LmsHelper
{   
    private static $host;
    private static $api_key;
    private static $api_version;
    private static $use_ssl;
    private static $debug;
    private static $port;

    public function __construct() {
        self::$host         = Config::get('lms.host');
        self::$api_key      = Config::get('lms.api_key');
        self::$api_version  = Config::get('lms.api_version');
        self::$use_ssl      = Config::get('lms.use_ssl');
        self::$debug        = isset(Config::get('lms.debug')) ? Config::get('lms.debug') : false;


        if (isset(Config::get('lms.port'))) {
            self::$port = Config::get('lms.port');
        } else {
            self::$port = self::$use_ssl ? 443 : 80;
        }
    }
}

and app\config\lms.php

<?php

return array( 
    'host' => 'www.themartialarts.university', 
    'api_key' => '6027-178512-e272b9afac4763199fa8d79e4bc0dcb39a378658', 
    'api_version' => '1', 
    'use_ssl' => true,
);

any idea what other method I can do? cause this is from normal PHP and I convert it into Laravel.

Upvotes: 1

Views: 4876

Answers (5)

user6029681
user6029681

Reputation:

The " isset " takes only variable. You must set the result of your function in a prior varriable.

 public function __construct() {
    $config = Config::get('lms.debug');
    self::$host         = Config::get('lms.host');
    self::$api_key      = Config::get('lms.api_key');
    self::$api_version  = Config::get('lms.api_version');
    self::$use_ssl      = Config::get('lms.use_ssl');
    self::$debug        = isset($config) ? Config::get('lms.debug') : false;

    $configPort = Config::get('lms.port');
    if (isset($configPort)) {
        self::$port = Config::get('lms.port');
    } else {
        self::$port = self::$use_ssl ? 443 : 80;
    }
}

Upvotes: 0

Niclas Larsson
Niclas Larsson

Reputation: 1317

You can only call isset on $variables.

Change that line to self::$debug = Config::get('lms.debug', false); to solve the problem. (set the default value to false)

Upvotes: 1

Ravi Hirani
Ravi Hirani

Reputation: 6539

Use if instead of isset as isset is used to check a variable is set and is not NULL.

Upvotes: 0

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

Even if the language construct supported it, it doesn't make much sense to test whether the function returns a variable because even missing return clauses generate a proper null. E.g.:

function nothing() {
}

var_dump( nothing() );
NULL

I suspect you want to use is_null() rather than isset().

Upvotes: 0

Philip Rollins
Philip Rollins

Reputation: 1291

Use the Config::has("name") method call since it's a method call you can't use isset().

Upvotes: 3

Related Questions