ivanh Posted November 3, 2009 Share Posted November 3, 2009 hi guys, i've had this problem for a while now anyhow i've this is my code user.php class user { var name = ""; function user($n) { $this->name = $n; } function getName() { return $this->name; } util.php include('user.php'); page1.php include('util.php'); $user = new user("ivanh"); page2.php include('util.php'); print $user->getName(); in page2.php i get this error: Fatal error: Call to a member function getName() on a non-object in page2 please let me know if you need anything else required to help me thanks, Ivan Quote Link to comment https://forums.phpfreaks.com/topic/180195-global-objects/ Share on other sites More sharing options...
Mchl Posted November 3, 2009 Share Posted November 3, 2009 You never instantiate $user in page2.php (or any of it's includes) Quote Link to comment https://forums.phpfreaks.com/topic/180195-global-objects/#findComment-950575 Share on other sites More sharing options...
ivanh Posted November 3, 2009 Author Share Posted November 3, 2009 ahhh, i just added $user = new user('ivanh'); into util.php so that everypage that includes util.php has this object. however, im still getting the same error. im also wondering, if i do this. does this mean im making new objects or its the same one? Quote Link to comment https://forums.phpfreaks.com/topic/180195-global-objects/#findComment-950580 Share on other sites More sharing options...
Mchl Posted November 4, 2009 Share Posted November 4, 2009 If you do what? Each time you run a script, it starts from scratch. No variables that have been created in previous runs are being carried over. Quote Link to comment https://forums.phpfreaks.com/topic/180195-global-objects/#findComment-950822 Share on other sites More sharing options...
PFMaBiSmAd Posted November 4, 2009 Share Posted November 4, 2009 This might be a good time for some web server 101 - Web servers are stateless. They don't know or care what happened before or what will happen next. When any http/https request has been serviced, all resources used on the page that was requested are destroyed. The only information that ties any page request to any other page request is what the browser supplies when it makes the http/https request to the server. You would need to create an instance of your class in a session variable if you wanted it to persist between any separate http/https page requests. Quote Link to comment https://forums.phpfreaks.com/topic/180195-global-objects/#findComment-950927 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.