Jump to content

Object Scope Help


tomast

Recommended Posts

Hi everyone,

I am fairly new to PHP but not to OOP, I was wondering if I could get some help with a problem I have come up against. I was under the impression that the <? and ?> were not a factor in the scope of variables and objects in this language, for example if I have:

 

<?php $myvar = 1; ?>[some HTML]<?php echo $myvar; ?>

 

It still seems to come out as 1. The problem I am getting is when I do this:

<?php
$myobj = new SomeObject();
$myobj->DoSomeFunction();
?>
[Lots of HTML]
<?php
$myobj->DoSomeOtherFunction();
?>

This gives me an error

Call to a member function DoSomeOtherFunction() on a non-object

I tried putting "global" in the constructor/declaration, I tried global when accessing it the second time, I tried $this-> before it. None of these work and I dont know what else I can do, I need the object both at the top and bottom of my html and I dont really want to have to construct two of them at each end just because of this.

 

Surely there is a way I can access the object defined at the top from the code block at the bottom?

Link to comment
https://forums.phpfreaks.com/topic/150925-object-scope-help/
Share on other sites

Thanks for such a speedy reply. I don't think I am overwriting it, my exact code is as follows:

<?php
require_once 'MyDatabaseClass.php';
$dbase = new MyDatabaseClass('localhost','username','password');
$dbase->SelectDatabase('database');

$q = $dbase->DoEscapedQuery('SELECT * FROM users WHERE uid='.$_POST['uid'].' LIMIT 1');
$r = $dbase->GetRow($q);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $r[1]; ?>’s Details</title>
</head>

<body>
<table width="100%" height="100%" border="1">
  <tr>
    <td width="100%" height="100%"><div align="center"><img src="bg.gif" width="629" height="228" /></div></td>
  </tr>
  <tr>
    <td width="100%" height="100%"><table width="100%" border="1">
      <tr>
        <td>menu item placeholder</td>
        <td>menu item placeholder</td>
        <td>menu item placeholder</td>
        <td>menu item placeholder</td>
        <td>menu item placeholder</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td width="100%" height="100%">
<?php
foreach($r as $i)
{
  echo $i.'<br/>';
}
?>
</td>
  </tr>
</table>
<div>
<?php
$q=$dbase->EscapedQuery('SELECT uid FROM users WHERE uid > '.$_POST['uid'].' LIMIT 1');
$r=$dbase->GetRow();
echo '<a href="users.php?uid='.$r[0].'>Next User >></a>';
$dbase->CloseDatabase();
?>
</div>
</body>
</html>

 

So as you can see, I dont overwrite $dbase anywhere and yet at the end, when making the link for the next user I just get the error that I showed in my post above. If I move that code to the part at the top then it works fine, but obviously the link is in the wrong place. The only thing I can think of now is to put the link in a variable and output it at the end, rather than using the database class I made, but this seems like a waste since I may as well have just used non-object oriented if that is the case.

Link to comment
https://forums.phpfreaks.com/topic/150925-object-scope-help/#findComment-792929
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.