Jump to content

Trigger functions from included file doesn’t work


isellsoap
 Share

Recommended Posts

The title of this thread isn’t very precise (or correct), sorry for that. Also: possibly this is the wrong section of the forum. I think this is a more general PHP programming question, that’s why I put it here.

  • I have a module that triggers a function after a page is saved in the backend.
  • That function includes a file from a folder in the same directory.
  • After the include I want to trigger a function from the included file.

So the basic setup is this:

<?php

class MyAwesomeModule extends WireData implements Module {
	
	…

	public function init() {
		$this->pages->addHookAfter( "save", $this, "myFunction" );
	}

	public function myFunction( $event ) {
		…
		include "inc/file.inc";
		functionFromIncludedFile(…);
	}

	…

	public function bla1(…) {
		…
	}

}

The file.inc file uses functions, that are declared in the module file, like so:

<?php

function functionFromIncludedFile() {
	…
	$var = $this->bla1(…);
	…
}

After saving a file I get "Using $this when not in object context" error. I know why the error occurs, but I don’t know how to solve my problem. Obviously, changing

$var = $this->bla1(…);

to

$var = bla1(…);

generates a "Call to undefined function bla1()" error.

I guess this is easily solvable, thanks for any help.

Link to comment
Share on other sites

First of all, it sounds like you're doing something wrong, but I can't really say that for sure without seeing the whole context. Why do you need to split your class into multiple files? Typically classes are meant to encapsulate certain strongly interrelated features into one package, which is why this sounds like you're mixing different concepts together.. and I'm afraid the outcome of that won't be pretty :)

Now, the easiest way would be to just drop the include or make it completely self-sufficient. Otherwise you'll probably either need to instantiate MyAwesomeModule inside that included file of yours and call bla1() from there or change bla1 into a static method and use it without actually instantiating MyAwesomeModule.

If these don't fit your needs, you might be able to achieve this by sending instance of current class as a param for your included function -- functionFromIncludedFile($this). This way you could call it's public methods pretty much like you've described above.

Link to comment
Share on other sites

I do financial calculations of companies. Every included file (respectively the function in the included file) represents such a company. So the functions in the module are more general functions which are being used by the companies, and the company function itself (from the included file) does highly specific calculations (because every company is different).

To sum it up: I don’t have to split the class into multiple files, but it does help with the overview when you don’t have to scroll through one massive file (think of 1,000 or 10,000 companies).

Edit: Eventually I want to do all the calculation stuff on the UI level of the backend. So I’d add pages for a company which represents additions, substractions and other predefined mathematical functions of certain fields. The module would iterate through those pages and then put everything together. That would a) be the perfect solution for me (also the most dynamic and elegant one, of course) and b) solve my above mentioned problem, which simply wouldn’t exist anymore  …

Link to comment
Share on other sites

I don't see anything wrong in doing so what you do include/load a php in a function. But usually it's another class or library. The scope on $this, doesn't exists from in that. So kinda with teppo that it might not easy to deal with it how you go with it depending on what methods you call and for what.

instead of $this in the included function

if would be more like

$module = new MyAwesomeModule();
$var =  $module->bla1(…);

or

$module = wire("modules")->get("MyAwesomeModule");
$var =  $module->bla1(…); 
Link to comment
Share on other sites

If I do that I get an "Call to a member function bla1() on a non-object" error.

If I call the functionFromIncludedFile function (the one in the module) with "$this->" at the beginning, I get an "Method MyAwesomeModule::functionFromIncludedFile does not exist or is not callable in this context" error.

Link to comment
Share on other sites

instead of $this in the included function

if would be more like

$module = new MyAwesomeModule();
$var =  $module->bla1(…);

or

$module = wire("modules")->get("MyAwesomeModule");
$var =  $module->bla1(…); 

Exactly what I was referring to above as instantiating MyAwesomeModule in the included file :)

Taking another look at this situation: in case that you need any class variables of the original MyAwesomeModule instance, you'd probably be better off sending it ($this) to your included function as a param as explained in my previous reply.. and you might even want to send the $event object as another param, in case that you need to do something there that's related to the page you're saving:

// in MyAwesomeModule:
functionFromIncludedFile($this, $event);

// and then in that function in your include file:
function functionFromIncludedFile($awesome, $event) {
    $awesome->bla1();
    $page = $event->arguments[0];
    // etc.
}

This still sounds like something you could do without dependencies going all over the place (first class depending on second class depending on methods / variables of first class..) but I won't go there as I still don't know nearly enough of your needs :)

  • Like 1
Link to comment
Share on other sites

include "inc/file.inc";

Make sure that you are more specific with your includes than this. If there was an "inc" directory off of the directory where your module is installed, then you'd want to do this:

include($this->config->paths->MyAwesomeModule . 'inc/file.inc'); 

or this

include(dirname(__FILE__) . '/inc/file.inc'); 
  • Like 3
Link to comment
Share on other sites

 Share

×
×
  • Create New...