[PHP] PHP5 Class problem
I'm using PHP 5.0.3 and if a problem if a class method. I'm initializing a class like that: $o_SessionHandler = new SessionHandler(); var_dump($o_SessionHandler-getOrgSession());exit; Now it get the error message: Fatal error: Using $this when not in object context in /srv/www/htdocs/SessionHandler.class.php on line 68 I try to debug that but that looks like a bug in the parser. The class looks like that: ?php //-- interface for SessionHandler Class interface SessionHandlerI { //-- functions static public function checkTime(); static public function checkSession(); static public function defineVar(); static public function getVar(); static public function isLogined(); static public function getOrgSession(); } //-- define SessionHandler Class class SessionHandler implements SessionHandlerI { //-- define var that keeps the original Session alive private $a_session_org; //-- initialize function public function SessionHandler() { $this-a_session_org = $_SESSION; } //-- function that checks how long the user don't do something static public function checkTime() { //-- check if time var exists if(isset($_SESSION['time_last_clicked'])) { //-- check the time differents if(time() - $_SESSION['time_last_clicked'] TIME_LOGOUT_IF_NO_CLICK) { //-- set new time $_SESSION['time_last_clicked'] = time(); } else { //-- block to login site Blocker::BlockToLoginSite(); } } else { //-- set var $_SESSION['time_last_clicked'] = time(); } } //-- function that checks if the Session Structur is correct static public function checkSession(){} //-- function that defines a var in the Session static public function defineVar(){} //-- functino that returns a session var static public function getVar(){} //-- check if the user is logined based on the session static public function isLogined(){} //-- returns the original login static public function getOrgSession() { //-- return var return $this-a_session_org; } } The getOrgSession() function is inside of a class so the $this should be avaible... any ideas? thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Class problem
Thomas Munz wrote: I'm using PHP 5.0.3 and if a problem if a class method. I'm initializing a class like that: $o_SessionHandler = new SessionHandler(); var_dump($o_SessionHandler-getOrgSession());exit; ... //-- returns the original login static public function getOrgSession() { //-- return var return $this-a_session_org; } } The getOrgSession() function is inside of a class so the $this should be avaible... $this is defined only if the following 2 things are true. 1. you are calling the method on a object (i.e. not as a static call like SessionHandler::getOrgSession()) 2. the function (method) you are calling is _NOT_ defined as static. in your case you have defined all your methods as static so the engine will not make $this available even if you call the method/function on an instantiated object. solution - remove 'static' from the function definitions that you wish to use $this in. any ideas? thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Class problem
1. you are calling the method on a object (i.e. not as a static call like SessionHandler::getOrgSession()) 2. the function (method) you are calling is _NOT_ defined as static. in your case you have defined all your methods as static so the engine will not make $this available even if you call the method/function on an instantiated object. solution - remove 'static' from the function definitions that you wish to use $this in. how odd, i have assumed having a class static you could still throw around variables inside it, or its only meant to stay in the one static method so executing it like Class::staticMethod(); and Class::otherStaticMethod() ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Class problem
[EMAIL PROTECTED] wrote: 1. you are calling the method on a object (i.e. not as a static call like SessionHandler::getOrgSession()) 2. the function (method) you are calling is _NOT_ defined as static. in your case you have defined all your methods as static so the engine will not make $this available even if you call the method/function on an instantiated object. solution - remove 'static' from the function definitions that you wish to use $this in. how odd, i have assumed having a class static you could still throw around variables inside it, or its only meant to stay in the one static method so executing it like Class::staticMethod(); and Class::otherStaticMethod() ? #2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Class problem
[EMAIL PROTECTED] wrote: 1. you are calling the method on a object (i.e. not as a static call like SessionHandler::getOrgSession()) 2. the function (method) you are calling is _NOT_ defined as static. in your case you have defined all your methods as static so the engine will not make $this available even if you call the method/function on an instantiated object. solution - remove 'static' from the function definitions that you wish to use $this in. how odd, i have assumed having a class static you could still throw around classes cannot be defined as static - defining them as abstract has the effect of being able to only use a given class statically (unless you subclass it and the subclass is not abstract). variables inside it, or its only meant to stay in the one static method so yes you can use variables - but not member variables because $this is not defined in functions that are declared static - bare in mind you can call a method statically even though its not marked as static (just be sure you don't reference $this). sidenote: the php4 trick of overwriting $this does not work in PHP5. executing it like Class::staticMethod(); and Class::otherStaticMethod() ? I don't really understand what your asking. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Class problem
electroteque wrote: On 29/01/2005, at 12:02 AM, Jochem Maas wrote: classes cannot be defined as static - defining them as abstract has the effect of being able to only use a given class statically (unless you subclass it and the subclass is not abstract). abstract as in it is the final base class ? no abstract as in 'cannot be instantiated' try running the following: abstract class MyClass { public function __construct() { echo boo!; } } $mc = new MyClass; the 'final' keyword is used to declare that a class or method cannot be overridden by (or in) a subclass. variables inside it, or its only meant to stay in the one static method so yes you can use variables - but not member variables because $this is not defined in functions that are declared static - bare in mind you can call a method statically even though its not marked as static (just be sure you don't reference $this). i meant member vars , what is the point of static methods anyway ? difficult question. a start would be to say that static methods offer a neat way to organise your code (sort of cheapmans namespace). PHP5 also offers static class vars: abstract class MyClass { static private $val; static public function set($v) { self::$val = $v; } static public function speak() { if (!is_null(self::$val)) { echo self::$val.\n; } else { echo silence\n; } } } MyClass::set(yeah); MyClass::speak(); sidenote: the php4 trick of overwriting $this does not work in PHP5. that i cant live with good ;-) ... the reason it doesn't work is the same reason PHP5 objects are s much cooler, namely a rewritten object model. thank the gods! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Class problem
[EMAIL PROTECTED] wrote: 1. you are calling the method on a object (i.e. not as a static call like SessionHandler::getOrgSession()) 2. the function (method) you are calling is _NOT_ defined as static. in your case you have defined all your methods as static so the engine will not make $this available even if you call the method/function on an instantiated object. solution - remove 'static' from the function definitions that you wish to use $this in. how odd, i have assumed having a class static you could still throw around variables inside it, or its only meant to stay in the one static method so executing it like As I understand it... It's not that you can't use any variables at all -- It's that $this doesn't make any sense in that context. $this refers to the particular instance of the object you have created with new XYZ $xyz = new XYZ(); $xyz-bar(); When bar refers to $this it means the same as the $xyz instance. If you are calling a static method XYZ::foo() then, really, there *IS* no object inside of foo() to be called $this -- You didn't create an instance of XYZ, so there is no instance to be named $this -- There is only the class definition and the method. It's an object that's not there -- more like the idea of an object without having an actual concrete object to hang on to. Hope that helps make sense of it all. Hell, hope that's actually correct! :-^ -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP5 Class problem
Richard Lynch wrote: [EMAIL PROTECTED] wrote: ... how odd, i have assumed having a class static you could still throw around variables inside it, or its only meant to stay in the one static method so executing it like As I understand it... It's not that you can't use any variables at all -- It's that $this doesn't make any sense in that context. $this refers to the particular instance of the object you have created with new XYZ $xyz = new XYZ(); $xyz-bar(); When bar refers to $this it means the same as the $xyz instance. If you are calling a static method XYZ::foo() then, really, there *IS* no object inside of foo() to be called $this -- You didn't create an instance of XYZ, so there is no instance to be named $this -- There is only the class definition and the method. It's an object that's not there -- more like the idea of an object without having an actual concrete object to hang on to. Hope that helps make sense of it all. Hell, hope that's actually correct! :-^ its correct. only thing it gets a little more complicated, personally I kind of got lost in all the internals discussion surround this behaviour... I just code so that I don't use or accidentally trigger it... in some cases this means using a protected/private method for an object. It easiest to see what I'm talking about if you run some code (tested php5): class XYZ { function doit() { var_dump( isset($this)); } } class ABC { function doit() { var_dump( isset($this)); } function doMore() { XYZ::doit(); $this-doit(); ABC::doit(); } } XYZ::doit();echo--\n; ABC::doit();echo--\n; $x = new XYZ; $a = new ABC; $x-doit();echo--\n; $a-doit();echo--\n; $a-doMore(); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php