PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?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()?
<?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...
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.
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.
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.
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.
<?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?
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....