Aureole Posted October 14, 2007 Share Posted October 14, 2007 I seem to get this error all the time, I'm pretty sure I'm not doing anything wrong but as always I will have missed something. Fatal error: Call to a member function on a non-object in /home/veraci7y/public_html/rev/admin.swr3 on line 127 Line 127 is: $html->menu_item('url.php', 'title', 'test'); The class has been instantiated correctly, I think... require('classes/class_html.swr3'); $html = new html; Here's the class: <?php class html { var $menu_url; var $menu_title; var $menu_text; var $output; function menu_item($url, $title, $text) { $this->menu_url = $url; $this->menu_title = $title; $this->menu_text = $text; $this->output = '<div class="menu_item"><a href="'.$this->menu_url.'" class="menu_item_a" title="'.$this->menu_title.'">'.$this->menu_text.'</a></div>'; print $this->output; } } ?> ??? Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/ Share on other sites More sharing options...
trq Posted October 14, 2007 Share Posted October 14, 2007 You need to use the .php file extension instead of swr3 (whatever that is). You could also setup your server to parse swr3 files as php but that will prevent your code from being portable. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369112 Share on other sites More sharing options...
Aureole Posted October 14, 2007 Author Share Posted October 14, 2007 It is already setup so that .swr3 files are parsed as .php or are you saying the class MUST be .php? My entire site has the extension .swr3 and it is parsed as PHP. I did it in .htaccess and my host modified httpd.conf just to make sure. So if it's not that what could the problem be... my database and member classes are .swr3 and they work. EDIT: Also the code doesn't need to be portable as the code is for a project that will not leave my hands. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369114 Share on other sites More sharing options...
trq Posted October 14, 2007 Share Posted October 14, 2007 If you've setup swr3 files to parse as php then there shouldn't be a problem. Your code works fine on my server. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369115 Share on other sites More sharing options...
Aureole Posted October 14, 2007 Author Share Posted October 14, 2007 That's extremely strange, perhaps there is something earlier on in the file interfering? Thanks a lot anyhow, I'll go investigate. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369117 Share on other sites More sharing options...
Aureole Posted October 14, 2007 Author Share Posted October 14, 2007 I'm using functions in a switch statement like case 'main' would run the function main() the $html->menu_item(); code is within the function main(), would that cause a problem? EDIT: I tried doing $html->menu_item(); outside of the function and it worked, well that explains that. Question is, how can I get it to work within the function... I don't want to resort to using included files for my $_GETs - there's lots of them. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369121 Share on other sites More sharing options...
trq Posted October 14, 2007 Share Posted October 14, 2007 would that cause a problem? Yes, because the $html object does not exist within said function. You'll either need to pass it to the function (recommended)... <?php $html = new html; // more code... // call to main. main($html); ?> Or make it a global within the main function definition (not recommended). You need to have a look at how variable scope works in php. Basically, any variables within a function only exist within that function. Variables created outside a function do not exist within said function. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369124 Share on other sites More sharing options...
Aureole Posted October 14, 2007 Author Share Posted October 14, 2007 Thanks, I tried the following after I instantiated the class... main($render); And I got a bunch of errors: Warning: mysql_query() [function.mysql-query]: Access denied for user 'veraci7y'@'localhost' (using password: NO) in /home/veraci7y/public_html/rev/inc/header.swr3 on line 22 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/veraci7y/public_html/rev/inc/header.swr3 on line 22 Access denied for user 'veraci7y'@'localhost' (using password: NO) I tried putting it within the function itself i.e. : function main($render) { // ... And I got the following error: Warning: Missing argument 1 for main() in /home/veraci7y/public_html/rev/admin.swr3 on line 65 Sorry but what did I do wrong now? Also is it possible to pass say all of my classes into the function so they can all be used, like: main($database, $member, $render); ...or...: function main($database, $member, $render) { // ... Thanks. Edit: I changed it to render as you can see, just so you don't get confused... Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369127 Share on other sites More sharing options...
trq Posted October 14, 2007 Share Posted October 14, 2007 Your not getting a database connection, the error says it all. Make sure your using the correct username / password combination and that if they are being included they are actually getting included. As for your other questions, we'll need to see some actual code. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369149 Share on other sites More sharing options...
Aureole Posted October 14, 2007 Author Share Posted October 14, 2007 I am getting a database connection... but the main(); was interfering with it but that's what you said to put. ??? I'll post some code in a second anyhow. EDIT: This is the file in question, I put comments in for you to follow. <?php session_start(); require('config.swr3'); require('classes/class_database.swr3'); require('classes/class_member.swr3'); require('classes/class_render.swr3'); $database = new database; $member = new member; $render = new render; $current_page = 'Test'; $database->connect(); // I tried putting what you said here. // main($render); // That gave me the database connection errors. $member->is_logged_in(); $member->is_supermoderator(); $member->is_administrator(); if(SYSTEM_OFFLINE == 1) { if($is_logged_in !== 1 || $is_supermoderator !== 1 || $is_administrator !== 1) { header('Location: offline.swr3'); } } if($is_logged_in == 1) { $member->last_action(); $member->current_page($current_page); } if(SYSTEM_SHOW_PHP_ERRORS == 1) { error_reporting(E_ALL); } switch($_GET['act']) { case 'main'; main(); break; case 'members'; members_and_groups(); break; case 'forums'; cats_and_forums(); break; case 'stats'; stats(); break; case 'logs'; logs(); break; case 'settings'; settings_and_tools(); break; default: main(); break; } function main() { // If I put // $render = new render; // Then it works... // But I don't see why I should have to instantiate it twice. ??? $pagetitle = 'ACP - Home'; require('inc/header.swr3'); $render->breadcrumbs('admin.swr3?act=main', 'ACP', '', 'Home', '', '', '', '', '', ''); $render->clearfix(); ?> <div class="leftcolumn"> <div class="con_2_outer"> <div class="con_2_title"><h1>Quick Links</h1></div> <div class="con_2_inner"> <p class="content"></p> </div> </div> <div class="con_2_outer"> <div class="con_2_title"><h1>Statistics</h1></div> <div class="con_2_inner"> <p class="content"></p> </div> </div> <div class="con_2_outer"> <div class="con_2_title"><h1>Admin Chat</h1></div> <div class="con_2_inner"> <p class="content"></p> </div> </div> </div> <div class="rightcolumn"> <div id="menu_outer"> <div id="menu_inner"> <?php $render->menu_title_first('Overview'); $render->menu_item('', '', SYSTEM_NAME .' v'. SYSTEM_VERSION); if(SYSTEM_OFFLINE == 1) { $render->menu_item('', '', SYSTEM_NAME .' is Offline'); } else { $render->menu_item('', '', SYSTEM_NAME .' is Online'); } if(SYSTEM_DEBUG == 1) { $render->menu_item('', '', 'Debug mode is On'); } else { $render->menu_item('', '', 'Debug mode is Off'); } $render->menu_title('To-do'); $render->menu_item('', '', 'Hurray! No To-do notes here.'); $render->menu_title('ACP Sections'); $render->menu_item('admin.swr3?act=main', '', 'Main'); $render->menu_item('admin.swr3?act=members', '', 'Members & Groups'); $render->menu_item('admin.swr3?act=forums', '', 'Categories & Forums'); $render->menu_item('admin.swr3?act=stats', '', 'Statistics'); $render->menu_item('admin.swr3?act=logs', '', 'Logs'); $render->menu_item('admin.swr3?act=setings', '', 'Settings & Tools'); ?> </div> </div> </div> <?php } function members_and_groups() { $pagetitle = 'ACP - Members & Groups'; include('inc/header.swr3'); //... } function cats_and_forums() { $pagetitle = 'ACP - Forums & Categories'; include('inc/header.swr3'); //... } function stats() { $pagetitle = 'ACP - Statistics'; include('inc/header.swr3'); //... } function logs() { $pagetitle = 'ACP - Logs'; include('inc/header.swr3'); //... } function settings_and_tools() { $pagetitle = 'ACP - Settings & Tools'; include('inc/header.swr3'); //... } $render->clearfix(); include('inc/footer.swr3'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369154 Share on other sites More sharing options...
trq Posted October 14, 2007 Share Posted October 14, 2007 In your function definition, you need to accept the $render object as an argument. function main() { Then when you call main() (within your switch) you need to pass it the $render object. main($render); Did you read the link I provided you about scope? You really need to learn about function scope well before you attempt to use classes. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369157 Share on other sites More sharing options...
Aureole Posted October 14, 2007 Author Share Posted October 14, 2007 I read up on it yes but I need to take it all in, one thing I'm wondering is I made a variable global within a function but it doesn't actually seem to be global.... <?php function is_logged_in() { global $is_logged_in; if(isset($_SESSION['mem_id'])) { ($_SESSION['logged_in'] == 1) ? $is_logged_in = 1 : $is_logged_in = 0; } else { $is_logged_in = 0; } return $is_logged_in; } ?> Then I call this function in my admin file via $member->is_logged_in(); as you can see it returns $is_logged_in which has been defined as global, I then have an if in my header file (which is an include in the admin file)... if($is_logged_in == 1) { ...[/code ...but it doesn't work at all even though it IS (apparently) global. I'll go through the link you provided again anyway, thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369206 Share on other sites More sharing options...
trq Posted October 14, 2007 Share Posted October 14, 2007 Using the global word within a method of an obejct will not work. Even if it did, it kinda defeats part of the purpose of objects (to encapsulate code). This is the same reason I don't recommend using it within narmal functions either. Function are designed to encapsulate functionality and also help in keeping the global namespace clean. Quote Link to comment https://forums.phpfreaks.com/topic/73175-gosh-darn-it/#findComment-369229 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.