473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

include file into class, retain class scope

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.

Jun 11 '07 #1
2 7091
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 - sc*********@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

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

Jun 11 '07 #2
On Jun 11, 7:28 pm, Schraalhans Keukenmeester
<Schraalh...@the.spamtrapexample.nlwrote:
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

Jun 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tom | last post by:
I'm tying myself in knots trying to figure out variable scope with constants and include files. This is what I'm doing: A page (index.php) on my website includes a general purpose include file...
4
by: Will | last post by:
I know I am searching using the wrong words because I can't seem to find a simple answer on this. Here's what I want to do. helper.inc <!php var $fileName2; class helper { function...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
4
by: Carramba | last post by:
hello! Iam wondering how can I include file in class? is it possible? is it possible to create object inside another object? for instance if I create mysqli connection object inside another...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
8
by: The Cool Giraffe | last post by:
One thing i do know for sure. When one creates a CPP file, one needs to include the H file. Now, having said that, i wonder if there are some general hints, requirements or standard guide lines on...
5
by: jonathan184 | last post by:
Hi I am trying to access a log file and read the fikle into an array and split the file using the space as a delimiter but i get this error. Parse error: syntax error, unexpected $end in...
6
by: manontheedge | last post by:
I'm having trouble using MFC in Visual Studio 6. The problem I'm having is, say I have a text box that information is typed into. Once a button is clicked, this information is passed into code in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.