Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/207961-general-question-regarding-php-oop/
Share on other sites

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".

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.