user11890551
user11890551

Reputation:

Calling method without class instantiating it

So I'm wanting to ditch some methods in some code and would like to not use the class instantiating and use the methods directly - How would I be able to convert if (!Request::has_get('run')) { into just calling the has_get function?

run.php

if (!Request::has_get('run')) {
    echo 'Hello! This is Semirs Page';
} 

The method:

public static function has_get($key = '', $value = '')
    {
        return self::has_value($_GET, $key, $value);
    }

My attempt which is incorrect:

if (!isset($_GET(has_get['run'])) {
    echo 'Hello! This is Semirs Page';
}

Upvotes: 0

Views: 40

Answers (2)

Barmar
Barmar

Reputation: 781751

If you want to ditch the class, just turn all the static functions into ordinary top-level functions, and remove self:: when calling other methods in the class. So it becomes

function has_get($key = '', $value = '')
    return has_value($_GET, $key, $value);
}

function has_value($array, $key, $value) {
    return isset($array[$key]) && $array[$key] === $value;
}

Upvotes: 1

jacob13smith
jacob13smith

Reputation: 929

In the same .php file you want to use the function, just declare it:

function has_get($key = '', $value = '')
{
    return has_value($_GET, $key, $value);
}

I'm not sure what you Request::has_value() function looks like, but you would need to declare that as well.

Upvotes: 0

Related Questions