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

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

Post 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()?
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

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

Post by Dummkopf »

I don't have to - function A is the constructor! ;)

Nevertheless: Thx for you tip! I'll try something like that ...
Dummkopf
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 1:54 pm

Post 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 ... ;)
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

can you build a function inside of anothter function?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

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

Post 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.
Last edited by Dummkopf on Tue Jun 10, 2003 4:11 pm, edited 1 time in total.
Dummkopf
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 1:54 pm

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

Post by nielsene »

Assuming we can get your current design to work, how does that avoid having the functions parsed on every request?
Dummkopf
Forum Newbie
Posts: 21
Joined: Sat May 24, 2003 1:54 pm

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

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

Post 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.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

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

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

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