include file into class, retain class scope

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
My apologies, i'm not sure how to ask this question correctly, so i'll
give the examples as i go.

First, i *think* my problem is described here, but i may be

Second, here's my problem:

i have a class defined. Inside that class are several LONG functions
that are called based on the result of a start function:

class doMyWork {
  var $dog = "Rex";
  var $cat = "Fluffy";

  function start ($step2, $FILE) {
    # preliminary stuff

    require_once("$"); # Include the external file/
    $step2($step2); # Call to included external function
  } # END start()

# more doMyWork stuff

} # END doMyWork

The value of $step2 originates from a web form based on a user's
selection. Now, the focus is on the require_once(). "step2" is three
things at once: the name of an external file, the name of a function
WITHIN that file, and a string value i need to pass to the function.

In theory, i've included the file with my needed function into my
class, making it a member function of the class, and therefore able to
access properties like $this->cat and $this->dog.

However, this is not the case. An error message tells me i've tried to
use "this" when not in object context. And if i try $this->
$step2($step2), i receive "Call to undefined method doMyWork::

Can anyone help me out here? i'll clarify as best i can if needed. The
whole point of this is to organize my code, because as i said, these
"step2" functions are very long.

Re: include file into class, retain class scope

At Mon, 11 Jun 2007 21:56:51 +0000, joe t. let h(is|er) monkeys type:

Quoted text here. Click to load it

If you strip the function header from the included file, and include the
file in a function definition in the calling script you all inclued code
becomes part of the class definition, and can acces $this->var.

class aClass {
    private $number = 10;
    public function funcOne () {
        echo "In funcOne<br>";
    public function funcTwo () {
    include 'func2.php';

$anObj = new aClass();

    echo "In funcTwo<br>";
    echo $this->number;

In funcOne
In funcTwo

You _can_ include a complete function definition inside a class method,
even one with the same name, but it will be seen as a func outside the
class definition(and therefor not generate a function redefinition error)

Schraalhans Keukenmeester -
[Remove the lowercase part of Spamtrap to send me a message]

  "strcmp('apples','oranges') < 0"

Re: include file into class, retain class scope

On Jun 11, 7:28 pm, Schraalhans Keukenmeester
Quoted text here. Click to load it

i hadn't even considered that approach! And i'm pretty sure that will
be the best option, since all of these included functions are passed
the same parameters and return the same possible results, they just
perform different internal tasks.

Thank you!

Site Timeline