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
class Class {
var $string = 'Hi!';
// Konstruktor etc.
function func() {
function subfunc() {
echo "executing subfunc"; // is printed
echo $this->string; // isn´t printed
}
subfunc();
}
}
$c = new Class();
$c->func();
?>
Subfunc will be executed. (Even if it is loaded with include() )
But it doesn´t have access to the class-members ...
(AND I NEED THAT ACCESS !! )
I think what Volka was saying is that you should be looking at making a bunch of childs to extend a base, parent class.
When you instantiate each child it has access to all the parent methods and properties. The child and parent functions work together as a unit.
If you have individual childs for each of the actions you need to perform, you don't need to parse a pile of stuff that isn't being used (it may not actually matter anyway).
When you instantiate a child, don't forget to stick this in the child's constructor:
It also may be worth using some timestamps to measure the amount of time spend parsing the entire body of functions. On a fast server with loads of RAM you might find it not to be that big of a problem. There's that old quote about premature optimization....
Even if the time is too great, it might be better to look into using some of Zend's other PHP products to speed/chache things?
The subclasses method doesn't seem quite right for this. If there is a sub class for military-action X and another for construction-action Y and another for diplomacy-action Z. You might need to use say two of them on a given instance of the class. Now you can either do the combinatorial explosion of classes (and with multi-inheritence it'll be a real mess) or you have to create a class, do the first action, create a new class of the new type and copy the state from one to the other and then preform the second action. (IE a sibling-copy constructor)
Now the one thing I can think of is doing a Python-esque style thing. Include the functions as you're currently doing, inside the class function call, which will place the function in the global namespace. Give it a unique prefix such as classname_function. Include $this as its first argument. Inside the function you can use this regular as if it were a class and you get the "Just-In-Time-Parsing" of the needed include file.
I've already coded it with the included functions called with &$this as argument. It works fine, although it still doensn't seem to be the most elegant solution. (
Giving a game-function the whole main-object - that doesn't sound like nice OO )
Well that's how OOP is implemented "under the covers" in all languages that I know of. Yes a programmer you seldom need to know this, but.... Its not like its wrong. And you aren't giving it the whole main-object. You're giving it a reference so it's exactly like the a member class....