false74 Posted October 24, 2012 Share Posted October 24, 2012 Here is my problem/question: Say I have a class stored in a php file called 'myclass.php' and I have my main index php page 'index.php'. In that index.php page I include the 'myclass.php' file and create a variable as a new object of the class from myclass.php. However in the index page there are multiple php breaks <?php ?> <?php ?>, how can I use the variable that is of my class throughout the entire page? myclass.php: class MyClass { function MyClass() { ... } function foo() { ... } function bar() { ... } } index.php <html> <head> ... </head> <body> <?php include_once('myclass.php'); $thing = new MyClass(); ?> ... <?php $thing->foo(); ?> ... <?php $thing->bar(); ?> </body> </html> When trying to call $thing outside of the first php block I get an 'undefined variable' error. What is the scope of this variable, how can I have it shared amoungst all the php code blocks? Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/ Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 There's no reason why this isn't working the way you wrote it here. Are you creating $thing inside a function? Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/#findComment-1387496 Share on other sites More sharing options...
false74 Posted October 24, 2012 Author Share Posted October 24, 2012 Are you creating $thing inside a function? $thing is defined inside the first php code block, but cannot be accessed outside of that. Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/#findComment-1387500 Share on other sites More sharing options...
akphidelt2007 Posted October 24, 2012 Share Posted October 24, 2012 $thing is defined inside the first php code block, but cannot be accessed outside of that. Can you post your actual code. Because what you posted will work and you can access $thing outside the first code block. So your code must be doing something different. Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/#findComment-1387501 Share on other sites More sharing options...
false74 Posted October 24, 2012 Author Share Posted October 24, 2012 showdb.php <?php class DBManager { var $showthumbfile; var $dblocation; var $db; var $mediaroot; var $showtable; var $epsiodetable; function CityBeatDBManager() { $showthumbrootdir = "thumbs/"; } function connectDB() { try { $db = new PDO('sqlite:' . $dblocation); } catch( PDOException $e ) { die('Error connecting to show database'); } } //QUERIES function queryAllShowRows() { return $db->query('SELECT * FROM shows ORDER BY show_id DESC')->fetchall(PDO::FETCH_ASSOC); } } ?> index.php <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Test</title> <LINK REL=StyleSheet HREF="grey.css" TYPE="text/css" MEDIA=screen> </head> <body> <?php include_once("showdb.php"); $databse = new DBManager(); ?> <ul class="nav"> <li><a id="navbrowse" class="more" href="javascript:void(0);" onclick="toggleBrowse();">Our Programming</a></li> <li><a id="navabout" href="?page=about">About Us</a></li> </ul> </div> <div id="browse" class="close" tabindex=0 onclick="displayBrowse(false);" onblur="displayBrowse(false);"> <h1>Browse our programs</h1> <ul class="shownav"> <?php function listShows() { foreach($database->queryAllShowRows() as $row) { echo " <li> <a href='?show=" . $row['show_mediadir'] . "' class='show'>" . "<img src='" . $row['show_mediadir'] . "/mainthumb.jpg' />" . "<span>" . $row['show_title'] . "</span>" . "</a>" . "</li>"; } } listShows(); ?> </ul> </div> </div> <div id="wrapper"> <h1> <?php echo $databse->$dblocation; ?> </h1> </div> </body> </html> undefined variable when calling $database inside that foreach loop Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/#findComment-1387505 Share on other sites More sharing options...
akphidelt2007 Posted October 24, 2012 Share Posted October 24, 2012 You can't access a variable like that within the scope of a function. Why are you wrapping that foreach loop in a function in the first place? Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/#findComment-1387510 Share on other sites More sharing options...
ManiacDan Posted October 24, 2012 Share Posted October 24, 2012 You also spelled $database wrong. Read up on PHP's variable scope Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/#findComment-1387511 Share on other sites More sharing options...
false74 Posted October 24, 2012 Author Share Posted October 24, 2012 You can't access a variable like that within the scope of a function. Why are you wrapping that foreach loop in a function in the first place? Even when I remove the function and do this: It still is undefined. <?php foreach($database->queryAllShowRows() as $row) { echo " <li> <a href='?show=" . $row['show_mediadir'] . "' class='show'>" . "<img src='" . $row['show_mediadir'] . "/mainthumb.jpg' />" . "<span>" . $row['show_title'] . "</span>" . "</a>" . "</li>"; } ?> Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/#findComment-1387512 Share on other sites More sharing options...
false74 Posted October 24, 2012 Author Share Posted October 24, 2012 Actually. I'm just a dumbass. I can't spell for anything... Sorry for your time. $databse .... $database My mistake Link to comment https://forums.phpfreaks.com/topic/269864-variables-shared-among-breaks/#findComment-1387514 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.