fatfrank Posted January 2, 2008 Share Posted January 2, 2008 I suspect this is a spectaularly noob question, but here goes. I have a main page, index.php, and within the header I require_once a file that contains a class and then I create a new instance of the class. The actual contents of the page is held in a mysql db; I query the db, retrive the text, write it to a temp file and then include it in index.php, all done through a method of the class mentioned above. Now, given that set up, why can't I access any of the methods of the class on the included temp file? I thought it was a scope problem, but the error message I get of "Fatal error: Call to a member function someFunction() on a non-object in etc etc", would seem to indicate otherwise. Any ideas? Thanks Frank Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/ Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 This seems an odd way of doing things. Can we see some actual code? Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428200 Share on other sites More sharing options...
aschk Posted January 2, 2008 Share Posted January 2, 2008 Include some code and we'll have a closer look. For a second there I thought you were creating a class on the fly, and including that class in your index main content. On re-reading I see otherwise. Post the relevant files... Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428202 Share on other sites More sharing options...
fatfrank Posted January 2, 2008 Author Share Posted January 2, 2008 Hi Thanks for the replies, set up is like this ----index.php------ <head> require_once "my.class.php" $page = new Page($page_id); </head> <body> $page->writetext(); </body> -------my.class.php------ class Page { function __construct($page_id){ $this->page_id=$page_id; $select = "SELECT pages.page FROM pages WHERE pages.page_id='".$this->page_id."' LIMIT 1"); $query = mysql_query($select); $result = mysql_fetch_assoc($query); $this->page = $result['page']; } function writetext(){ file_put_contents("tmp.txt", $this->page); require_once("tmp.txt"); unlink("tmp.txt"); } function somefunction() { <!--some code to generate a site map--> } } ------Page in db------- <h1>Site map</h1> <?php $page->somefunction(); ?> ------------------------------- Hopefully that is clear. The code for somefunction isn't too important - I've tested it elsewhere and it works, plus I get a similar error messages when trying to use other functions in place of somefunction. Cheers Frank Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428217 Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 Logically I can't really see why you would be doing it this way, but I also can't see why it wouldn't work either. I'm not in the position to test anything at the moment so I'm not sure what the issue is exactly. It would help if you saved you page to a .php file instead of .txt, though because your getting a php error it would seem it is being parsed properly. I'm sorry, but without writting some tests I couldn't help. Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428223 Share on other sites More sharing options...
trq Posted January 2, 2008 Share Posted January 2, 2008 Actually, I think Ive found it. $page does not exist within the class itself. try changing the file in your db to be.... <h1>Site map</h1> <?php $this->somefunction(); ?> and see if you have any success. Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428227 Share on other sites More sharing options...
fatfrank Posted January 2, 2008 Author Share Posted January 2, 2008 Thorpe you are a wondeful person. Works a charm My reasoning behind putting all my content files in a db is to help me set a couple of secure pages - if the user's access level is below that required by a specific page, they get served a different one. Just seemed to be the easier way to manage it. Thanks again Frank Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428242 Share on other sites More sharing options...
aschk Posted January 2, 2008 Share Posted January 2, 2008 I would really advise against following this principle. It's messy, fraught with errors (as you have demonstrated), and will cause you no end of complication with maintenance. I can see what you're attempting to do, but application security (i.e. access level) can be achieved a different way. Don't embed $this/$page or otherwise into your presentation layer (as you are at the minute). Keep your logic separate from your presentation... Also, the notion of writing files onto your filesystem then unlinking (deleting) them is absurd (no offense intended). Keep everything in memory. Disk I/O is notoriously slow (in comparison to memory access). Also, imagine if 100 people view a page at roughly the same time, you've just asked your operating system to write 100 files to your hard disk, and then erase them... I like what you're doing, it's very clever, but just beware. It WILL cause you issues. Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428254 Share on other sites More sharing options...
fatfrank Posted January 2, 2008 Author Share Posted January 2, 2008 Thanks for the heads up aschk. Got so carried away with getting it to work that I forgot to think about anything else... The two issues you mention are linked. I need to be able to execute code within the content files so dumping it all into a temp file seemed the obvious way. I am very much the newbie so comments gratefully received. Back to the drawing board then ; ) Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428281 Share on other sites More sharing options...
aschk Posted January 2, 2008 Share Posted January 2, 2008 I love the fact that you're using objects for pages. You're definitely walking down the right road. Google MVC, hopefully it'll give you a better understanding of trying to split up your logic. Model = Data Objects View = presentation (html) Controller = The PHP code that decides what models to use and what views to present. Quote Link to comment https://forums.phpfreaks.com/topic/84122-solved-noobie-object-question-about-scope/#findComment-428321 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.