You are here: Re: include file into class, retain class scope « PHP Programming Language « IT news, forums, messages
Re: include file into class, retain class scope

Posted by Schraalhans Keukenmeester on 06/11/07 23:28

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

> 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
> misunderstanding:
> http://bugs.php.net/bug.php?id=15233&edit=1
>
> 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("$step2.inc.php"); # Include the external file/
> function
> $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::
> [value_of_step2]".
>
> 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.

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.

test.php:
<?PHP
class aClass {
private $number = 10;

public function funcOne () {
echo "In funcOne<br>";
}

public function funcTwo () {
include 'func2.php';
}
}

$anObj = new aClass();
$anObj->funcOne();
$anObj->funcTwo();
?>

func2.php:
<?PHP
echo "In funcTwo<br>";
echo $this->number;
?>

//output:
In funcOne
In funcTwo
10

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 - schraalhans@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация