|
Posted by joe t. on 06/11/07 23:55
On Jun 11, 7:28 pm, Schraalhans Keukenmeester
<Schraalh...@the.spamtrapexample.nl> wrote:
> 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 - schraalh...@the.Spamtrapexample.nl
> [Remove the lowercase part of Spamtrap to send me a message]
>
> "strcmp('apples','oranges') < 0"
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!
-joe
Navigation:
[Reply to this message]
|