Page 1 of 2
Extending class-functionality with include() -> Problem!
Posted: Tue Jun 10, 2003 1:31 pm
by Dummkopf
Code: Select all
<?php
// func.inc.php:
function test() {
echo $this->string;
}
// start.php
class A {
var $string = 'Hi';
function A() {
include('func.inc.php');
$this->test();
}
}
$A = new A();
?>
I know that this example doesn´t work.

(No 'Hi'

)
But what do I have to change in order to add a new function to my class with an include()?
Posted: Tue Jun 10, 2003 3:42 pm
by liljester
first of all i wouldnt suggest using A as a class name, object name, and function name all in the same script... its a bit confusing..
try something like this:
Code: Select all
<?php
// func.inc.php:
function test() {
echo $this->string;
}
// start.php
class A {
var $string = 'Hi';
function method_name() {
include('func.inc.php');
}
}
$object_name = new A();
$object_name->method_name();
?>
and to answer your question, you didnt call ("$A->A();") the A method from the A object... which is of class A...
Posted: Tue Jun 10, 2003 3:50 pm
by Dummkopf
I don't have to - function A is the constructor!
Nevertheless: Thx for you tip! I'll try something like that ...
Posted: Tue Jun 10, 2003 4:01 pm
by Dummkopf
If I code my project that way, there will be very many small files, everyone containing one function.
That would look and feel very unorganised! Additionally I sometimes have to call other functions from the included function. That doesn´t seem to be possible that way ...
I´d like to have several include-files, each of them containing several functions, which simply belong together. These functions have to have access to the members of the A-class via $this->etc.
There has to be a way ...

Posted: Tue Jun 10, 2003 4:03 pm
by liljester
can you build a function inside of anothter function?
Posted: Tue Jun 10, 2003 4:07 pm
by nielsene
Is there a reason you don't do something like
File: A.inc (or A.php or A.inc.php, depending on your preference)
Code: Select all
<?php
class A
{
var $string="Hi";
function A()
{
$this->test();
}
function test()
{
echo $this->string;
}
}
?>
File: start.php
Code: Select all
<?php
include("A.inc");
$A= new A();
?>
I guess I don't understand why you want to include all the files outside of the class defination. Can you explain your need?
Posted: Tue Jun 10, 2003 4:07 pm
by Dummkopf
@liljester:
Yes - in the example shown above I can call test() instead of $this->test() and get the expected result.
But that's not what I'd like to have ...
I want to have $this->test(); and the function 'test' has to have access to the class-members via $this->etc.
Posted: Tue Jun 10, 2003 4:10 pm
by Dummkopf
@nielsene:
This very simplified example normally is a class, which handels the actions of a web-based strategy-game - a complex one ...
I don´t want to have all the functions parsed every time a visitor/player starts a request!
Posted: Tue Jun 10, 2003 4:12 pm
by nielsene
Assuming we can get your current design to work, how does that avoid having the functions parsed on every request?
Posted: Tue Jun 10, 2003 4:17 pm
by Dummkopf
Of course the (!) function, which is supposed to be parsed, will be loaded. But all the others (propably over 200) won't be!
I want to load - let's say the code-snippet, which starts building a house. That action doesn't need the functions for ressource-gathering, military-tasks, etc.
Posted: Tue Jun 10, 2003 4:25 pm
by nielsene
Ahh got it.
So when you try your way, you say it doesn't work. Do you have error reporting turned on, to the highest level? If so you should be receiving some kind of error, can you let us know what it was. I suspect there is a warning or a notice being thown that will help us figure out a working solution.
Posted: Tue Jun 10, 2003 4:32 pm
by Dummkopf
I don't get any error-reportings, but I guess the problem is, that the function in the included file is a kind of sub-function. I can call test() instead of $this->test and it works fine. The sub-function cannot call the class-members via $this->etc. though.
I need an idea which allows me to make functions in include-files members of the class I call them from.
Instead of that I could build classes like FuncBuild and FuncRessources, which contain the needed functions. These classes would get all the members of the main-class in order to let them execute their actions. (DB-Object, Constant-Handling-Object, User-Object, Language-Object, Output-Object, Error-Handling-Object, and so on ...)
But I would the include-version consider more elegant and more logic.
Posted: Tue Jun 10, 2003 4:33 pm
by liljester
are you sure your file is being parsed? here is what i tried:
Code: Select all
<?php
/////////////
//start.php
/////////////
class A {
var $string = 'Hi';
function A() {
include("func.inc.php");
$this->test();
}
}
$methods = get_class_methods( A );
foreach($methods as $method)
{
print"$method<BR>\n";
}
/////////////////
//func.inc.php
////////////////
echo "i was included.<BR>\n";
function test() {
echo $this->string;
}
?>
all i got for output was:
"a"
if the file is being parsed, it should have output "i was included.<BR>\n"; also... yes?
Posted: Tue Jun 10, 2003 4:33 pm
by nielsene
Hmm it looks like anytime you do a function in-a function the "sub"-function belongs to the global namespace. Therefore it won't be considered part of the class. I'm heading out now, I hope someone else finds a good solution for you....
Posted: Tue Jun 10, 2003 4:38 pm
by Dummkopf
@liljester
I've tried that, too, when I thought about that problem myself and I got the whole message!
The include-file is included correctly in my project. But the sub-function isn´t a member of the class ...
There must be either a trick or a totally different solution ...
