1

I have problem with PHP class and function.

My class file is:

<?php
class EF_IP{

public function ip_adresa(){

    // dobivanje IP adrese
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
        return $ip;
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            return $ip;
                } else {
                    $ip = $_SERVER['REMOTE_ADDR'];
                    return $ip;
        }
    }
}
?>

And i call from other PHP file:

EF_IP::ip_adresa();
echo $ip;

And i get error:

Strict Standards: Non-static method EF_IP::ip_adresa() should not be called statically

What i need to do?

1
  • 1
    Change public function to public static function Commented Feb 22, 2017 at 14:55

2 Answers 2

2

You can either make your function STATIC or instantiate the class first:

class MyClass {
    public static function SomeFunction() {}
    public function someOtherFunction() {}
}

Then you call either like so:

MyClass::SomeFunction()
$class = new MyClass();
$class->someOtherFunction();
Sign up to request clarification or add additional context in comments.

Comments

1

Call your function not staticly:

$ef_ip = new EF_IP();
$ip = $ef_ip->ip_adresa();
echo $ip;

or you make your function static:

public static function ip_adresa(){
 // your code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.