linus72982 Posted July 16, 2010 Share Posted July 16, 2010 I have just a few quick beginner's questions regarding PHP OOP: 1. Why do all the scripts I read have the object instantiated after the class is closed at the end of the document? It seems intuitive to me to instantiate the object in the script on the page. 2. I don't really understand where variables (whether they be objects or otherwise) are "stored." I get the public and private and all that within a class, but what about between files in my directory? If I have, say, 4 different PHP files that contain their own classes, and then an index.php content file that actually displays things on a page, I've used "include()" to include all the 4 files and each file has the instantiator at the end of the file, does PHP instantiate those objects on the page load or do I have to call them first for it to be instantiated? If I set a variable, let's say $name, in file 3 when I called a certain function, is it available for use on the index page or do I explicitly have to return it from the function? Basically, how and where and in what scope does PHP store variables between files? Thanks for the help. - Adam Quote Link to comment https://forums.phpfreaks.com/topic/207961-general-question-regarding-php-oop/ Share on other sites More sharing options...
Mchl Posted July 16, 2010 Share Posted July 16, 2010 1) Because you're reading wrong scripts. 2) You're confused because of 1) Quote Link to comment https://forums.phpfreaks.com/topic/207961-general-question-regarding-php-oop/#findComment-1087146 Share on other sites More sharing options...
jcbones Posted July 16, 2010 Share Posted July 16, 2010 Any variable declared in an included page, is available on any other page in the include structure. Try this: Test1.php <?php include('Test2.php'); echo $test; ?> Test2.php <?php include('Test3.php'); ?> Test3.php <?php $test = 'This is only a Test'; ?> Now run "Test1.php". Quote Link to comment https://forums.phpfreaks.com/topic/207961-general-question-regarding-php-oop/#findComment-1087217 Share on other sites More sharing options...
Maq Posted July 16, 2010 Share Posted July 16, 2010 Basically, how and where and in what scope does PHP store variables between files? In OOP you can get/set values from objects with accessors methods. Quote Link to comment https://forums.phpfreaks.com/topic/207961-general-question-regarding-php-oop/#findComment-1087241 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.