Jump to content
isellsoap

Trigger functions from included file doesn’t work

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.

Share this post


Link to post
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.

Share this post


Link to post
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  …

Share this post


Link to post
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(…); 

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By David-M
      anyone, any ideas how to solve? 
      i get following error on a website:
      Notice: Undefined variable: out in /var/sites/i/islandmemorials.co.uk/public_html/site/templates/includes/functions.php on line 43
      and this is a code in that very functions file:
      <?php function numberOpen() { date_default_timezone_set("Europe/London"); $currentHour = date('G'); if($currentHour > 9 && $currentHour < 21) { $openFor = 21 - $currentHour; if($openFor > 1) { echo "We're currently open for another ".$openFor." hours, why not pop in?"; } else { echo "Our lines are open for another hour - <a href=\"tel:+01983 857 600\">01983 857 600</a>"; } } else { echo "Unfortunately, our lines are currently closed, but you can still send a message below"; } } function getSubNav($pid) { $test = wire('pages')->get("/services/"); $parents = wire('page')->parentID; $template = wire('page')->template; $out = ''; //echo "pid".$pid."<BR>"; //echo "parents".$parents."<BR>"; //echo "template".$template."<BR>"; $children = $test->children; foreach($children as $child) { $class = $child->id === $pid ? " active" : 'inactive'; if ($child->id === $pid ) { $class= 'active'; } elseif ($template= "memorials" && $child->id === $parents ) { $class= 'active'; } $out .= "<li><a class='{$class}' href='{$child->url}'>{$child->title}</a></li>"; } return $out; } ?> i suppose this has to do with a variable scope, but not sure
    • By Atlasfreeman
      Hi!
      I want to make a small site, a one page site.
      And i have this idea about doing 2 to 3 diffrent template that i can load into the index / home page.

      I want to do this with an array so that i can keep creating more topics (with the template) id needed.
      <?php include('./head.inc'); // include header markup ?> <?php $children = $page->get('template=onecolmn|twocolumn, sort=sort'); foreach($children as $child) { include($child); } ?> <?php include('./foot.inc'); // include footer markup ?>  
      As you can see i have to template wish i want to control from the backend.
      I know include does not work this way, put what is my other option to make an array that loads the whole page on an other pages.
       
       
    • By iipa
      Hi fellow developers!
      I want to implement following action:
      Admin-priviledged user uploads a spreadsheet file (with many sheets) When page is saved, back end generates repeater items from the sheets We can render spreadsheet data from the repeater field. Profit! Currently I'm trying to find a way to read the uploaded spreadsheet file. ProcessWire doesn't seem to have modules suitable for my needs, but external PHP Library PhpSpreadsheet sounds like it could do the job. However, I can't even try it, because I simply don't understand how I can refer to an external library!
      Folder structure:
      templates _func.php > where I want to refer to the library libraries PhpSpreadsheet bunch of *.php > what I want to refer to I have tried different things varying from their documentation to forum posts and PW API with no luck. I would prefer not to use Composer, since it seems kind of overkill for one library.
      Thanks in advance and have a great day!
    • By nuel
      Hi there
      Basically I want to call code within a ProcessWire page that isn't used as a template. Example: www.mypwpage.com/myphpfile.php
      I have a working PW Website with a couple of pages like /artists, /releases, /videos etc. Now I need a page /download without any editable fields in the backend, just calling some PHP code (that was coded by another guy) containing a form that checks unique download-codes in a second database and starts the download of the desired file. The script is working fine right now as part of a static website, but since I built PW behind the site, this independent «Download Section» of the page doesn't work anymore.
      Right now I have the main file download.php as a page template on a newly created empty page called /download, so until now the form is working (wow). After sending the form containing the download-code, the file check_code.php in a subfolder /site/templates/download is called and that's where I get an error.
      Any help?
    • By louisstephens
      I am wondering, how do you pass a variable into wire('page')->get() inside a function? I have been looking through the forums, but unfortunately I have not found the answer yet.
      My current set up is:
      function generateNewPages($parentPageName) { $p = new Page(); $p->template = "parent"; $p->parent = wire('pages')->get('/home/'); $p->name = $parentPageName; $p->title = $parentPageName; $p->of(false); $p->save(); $p2 = new Page(); $p2->template = "child"; $p2->parent = wire('pages')->get('$parentPageName'); $p2->name = "child"; $p2->title = "Child Page"; $p2->of(false); $p2->save(); } When I try to run it by passing in a title like generateNewPages('Demo');, "Demo" is created, but when it gets to the child page I get:
      Unknown Selector operator: '$' -- was your selector value properly escaped?
      Is there a way to pass the $parentPageName to "wire('pages')->get('$parentPageName')" ?
×
×
  • Create New...