1

I have just learned to use double colon in php. I have a class with many function inside:

class auth {

/* .... Other Functions ...*/

    public function logout ()
    {
        session_start();
        $_SESSION = array();
        if (ini_get("session.use_cookies")) {
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000,
                $params["path"], $params["domain"],
                $params["secure"], $params["httponly"]
            );
        }
        session_destroy();
    }

}

Now I included this auth.class.php in another file logout.php then called

auth::logout();

I have not found any error (except: "header already sent") when turning php error to E_ALL level.

I heard that scope resolution operator only works on static function. So I am in puzzle, please help me to understand it better... plz

hakre
  • 193,403
  • 52
  • 435
  • 836
Haren Sarma
  • 2,267
  • 6
  • 43
  • 72

6 Answers6

3

You can call a non-static function in a static way (like you do). In believe PHP 5.3 is somewhat stricter, but it's still possible. In this case it won't give any problems, because your function doesn't refer to any variables of the object itself, so it doesn't actually rely on the object instance. Therefor you won't get any errors.

But still, I would advice not to do this. Your code will become confusing, and it may (and should) break in future versions.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

That has more to do with your session_start().
You have to execute this function before any output to the browser (may it be templates, echos or even a whitespace before <?php).

You probably need it anyway on every page load, so I would suggest you place that somewhere in a config file which is loaded at the very beginning.

dan-lee
  • 14,365
  • 5
  • 52
  • 77
0

session_start(); should be part of your bootstrapping/initialization code, not part of the auth class. Put it somewhere along the first lines that get executed in your project. If you output ANYTHING before calling session_start() you'll get headers already sent errors and the results will be unpredictable.

ChrisR
  • 14,370
  • 16
  • 70
  • 107
0

You call your logout() method statically. So you need to add static keyword to your method definition like public static function logout().

header already sent error indicates that you have some data outputted to browser before Real headers were sent. It could be some php output or errors.

s.webbandit
  • 16,332
  • 16
  • 58
  • 82
0

PHP is very lax with static vs. non-static methods. If you turn on strict mode in your php config this will be error.

Also: "PHP 4 did not have a static keyword (in function declaration context) but still allowed methods to be called statically with ::. This continued in PHP 5 for backwards compatibility purposes."

See also: Calling non static method with "::"

Community
  • 1
  • 1
Julian Hollmann
  • 2,902
  • 2
  • 25
  • 44
0

First thing is here that you are using the scope resolution operator which only works with static function . Either you have to make this function static using static keyword after public then you can call with its class name you would not have to make a object of that class or you can make a object like this

<?php 
     $myobject    = new myclass();
?>

And one major think the session_start(); function should be the first line of your webpage because it sends the header info to the server. and server saves them. otherwise it will be a problem that resending the header info using the php function because every htm always sends a http header info to server so session_start should be the first line of any of the page

if this is relevant for you can mail me on info.gtensoft.in Senior Developer in Gtensoftwares www.gtensoft.in from new delji ,india