Jump to content

[SOLVED] Noobie object question about scope


fatfrank

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Thorpe you are a wondeful person. Works a charm :D

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ; )

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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