Cardale Posted May 17, 2009 Share Posted May 17, 2009 I was reading over some documentation about classes and had a question. When you put a object into a certain state it stays that way? So the value of that state could be used again in another instance? Like another script after the state has been set? Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/ Share on other sites More sharing options...
Ken2k7 Posted May 17, 2009 Share Posted May 17, 2009 Yeah, you can do that with static and possibly final. That is assuming you are using PHP 5. Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835630 Share on other sites More sharing options...
Cardale Posted May 17, 2009 Author Share Posted May 17, 2009 So say for instance I had a function like this <?php function test() { static $a = 0; echo $a; $a++; } ?> I could call this function in one page and it would set its value to 1 and then have a link to the next page which calls the function and it would display 2? Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835645 Share on other sites More sharing options...
Ken2k7 Posted May 17, 2009 Share Posted May 17, 2009 No, you said classes and objects, not functions. Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835646 Share on other sites More sharing options...
Cardale Posted May 17, 2009 Author Share Posted May 17, 2009 ok so if I put that into a class then it would work? Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835647 Share on other sites More sharing options...
Ken2k7 Posted May 17, 2009 Share Posted May 17, 2009 Try this - (I haven't tested it) <?php class A { private static $e = 0; public function __construct ($e = null) { if (!is_null($e)) A::$e = $e; } public function getE () { return A::$e; } } $e = new A(5); $f = new A; echo $f->getE(); Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835650 Share on other sites More sharing options...
Cardale Posted May 17, 2009 Author Share Posted May 17, 2009 I tested the function out using three separate pages. One for the class then another to set the value of e to 5 then the third file to get the value of e and it was still 0..I included the class in test2 and test3 pages. Is this not what you meant? Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835658 Share on other sites More sharing options...
Philip Posted May 17, 2009 Share Posted May 17, 2009 You need to use sessions or a database/flat file to continue using a value across multiple scripts. Static reads as following: Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can). Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835663 Share on other sites More sharing options...
Cardale Posted May 17, 2009 Author Share Posted May 17, 2009 I guess I need to just give up. I have been trying to figure out a way to catch a error and have it sent to another page without POST or GET methods. I save the error in a flat file didn't want to read that and wanted to avoid using a database, but I guess whats the difference in this instance. It would make things a lot simpler. should I use sessions for something like this? Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835677 Share on other sites More sharing options...
Ken2k7 Posted May 17, 2009 Share Posted May 17, 2009 There is the try...catch. <?php try { echo 5/0; } catch (Exception $e) { header('Location: www.google.com'); } Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835684 Share on other sites More sharing options...
Cardale Posted May 17, 2009 Author Share Posted May 17, 2009 I wanted to pass the error to a different page so it wouldn't look so horrible and have a half cut off page. try and catch works with errors and exceptions? I thought they were different.?? Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835689 Share on other sites More sharing options...
Ken2k7 Posted May 17, 2009 Share Posted May 17, 2009 You are absolutely right! Use set_error_handler Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835690 Share on other sites More sharing options...
Cardale Posted May 17, 2009 Author Share Posted May 17, 2009 Right right... Quote Link to comment https://forums.phpfreaks.com/topic/158450-solved-classes-and-objects/#findComment-835695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.