Extending class-functionality with include() -> Problem!

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!

Moderator: General Moderators

Dummkopf
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 1:54 pm

Post 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 ... :roll:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

why can't you use sub-classes?
Dummkopf
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 1:54 pm

Post 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 !! ;))
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
Dummkopf
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 1:54 pm

Post 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 ;))
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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....
Post Reply