problem in objects ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

problem in objects ??

Post by PHPycho »

Hello forums !!
i had a problem while creating a objects..
i usually create the objects at the top of the index.php page and all other files are included in the index.php page..
suppose i create a object say :

Code: Select all

$obj1 =  new MyClass();
while using this object in the another page say cat.php which is included in the index.php.

Code: Select all

if(isset($_POST['SUBMIT'])){
 global $obj1;
$obj1->save();
}
i had to make the object global, other it doesnt work..inside the functions its obvious to make the varible global in order to use it..but i had to use it in each if() condition.....
Any idea avoid this situation of making global in case of if(), while() etc conditions
Awaiting for your great help
Thanks in advance to all of you
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

PHP does not have a notion of scoping conditional structures, so global inside an if or loop is unnecessary. You will only need global when you're in a function.

Edit: Ah, I misread the question. That's weird.
Last edited by Ambush Commander on Sun May 27, 2007 7:29 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

is cat.php included within a function, something like

Code: Select all

function xyz() {
 include 'cat.php';
}
?

Which php version do you use?

Code: Select all

<?php echo phpversion(); ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I can't make any sort of possible culprit without more code being posted.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Ya i included the files using a function.
I used

Code: Select all

if(isset($_GET['page']){
 loadPHP($_GET['page']);
}

Code: Select all

loadPHP($page){
 include $page.".php";
}
Awaiting for your help.
thanks again
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

When a file is included inside a function, it inherits the function's scope, not the global one. So yes, global is necessary.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

thanks for the help..
I want to know if there is any other method to use functions and not to declare as global..
thanks again..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You could pass parameters to the function. The included script can access them without marking htme as global.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

PHPycho wrote:thanks for the help..
I want to know if there is any other method to use functions and not to declare as global..
thanks again..
At the top of your loadPHP() function...

Code: Select all

function loadPHP()
{
  foreach (array_keys($GLOBALS) as $k)
  {
    $$k =& $GLOBALS[$k];
  }
  //continue
}
I'd probably consider that bad programming though. If you're using OOP there's probably a better way to "globalise" your objects. Say for example a registry.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

can i know how you include the pages
ie using function or simply using include|require etc.
thanks a lot
Post Reply