Page 2 of 2
Posted: Tue Jun 10, 2003 4:52 pm
by Dummkopf
I've tried to make another example:
Code: Select all
<?php
// xxx.inc.php
// -------------
echo "I'm the include-file !!"; // printed correctly
function gameFunc1() {
echo "Start gameFunc1!"; // printed correctly
echo $this->string; // !!! No output !!!
}
// index.php
// -----------
class Main {
// Lots of objects
var $string;
function Main() {
// ...
$this->string = "Hi everybody!!";
}
function startAction() {
include(xxx.inc.php); // depending on the $action-value:
gameFunc1(); // sub-function gameFunc1 is called, BUT
// !!! $this->gameFunc1() doesn´t work !!!
}
}
$class = new Main();
$class->startAction();
?>
I hope it's easier to understand now ...

Posted: Tue Jun 10, 2003 5:07 pm
by volka
why can't you use sub-classes?
Posted: Tue Jun 10, 2003 5:12 pm
by Dummkopf
I tried that:
Code: Select all
<?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 !!

)
Posted: Thu Jun 12, 2003 12:10 am
by McGruff
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:
parent::NameofParentClass($any_args);
.. if you need to initialise parent properties.
Posted: Thu Jun 12, 2003 8:11 am
by nielsene
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.
Posted: Thu Jun 12, 2003 8:20 am
by Dummkopf
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

)
Posted: Thu Jun 12, 2003 8:24 am
by nielsene
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....