A problem using an external class in Datatables Editor

A problem using an external class in Datatables Editor

obrienjobrienj Posts: 93Questions: 38Answers: 0
edited August 2017 in Free community support

This problem may be due to an incomplete understanding of PHP but it files in the face of all the other programming languages I use and I need an explanation so I can use other external classes

I have an purpose defined class as follows:

<?php

namespace OTLogger;

class OTLogger {
 
    // Create a new constructor
    public function __construct() {
    }

    // write some text
    public function lwrite($message) {

        // do work here
        
    }
}

<?php
>
```
?>


And following is a shortened annotated Datatbles Editor PHP script.

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

require( "C:/wamp64/www/distLibs/phpLibs/DataTables-Editor/DataTables.php" );

require( "C:/wamp64/www/distLibs/phpLibs/OTLogger.php" );

use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,

use OTLogger\OTLogger;

$log = new OTLogger();

$log->lwrite("some text"); //<++++++++ this works

Editor::inst( $db, 'calendar.events', 'RID' )

->fields(
    Field::inst( 'RID' )->set(false),                           
    Field::inst( 'id' ),
    Field::inst( 'title' )->validator( 'Validate::notEmpty' ),
)

->on( 'preCreate',
    function ( $editor, $values ) {
        $log->lwrite("some text");           //<++++++++ this fails with because $log is null
    })

->on( 'postCreate',
    function ( $editor, $id, $values, $row ) {
        $log = new OTLogger();
        $log->lwrite("some text");          //<++++++++ this works because $log is instantiated locally

    })

->process( $_POST )

->json();

```

Put simply. I am trying to produce a "global $log" that can be accessed anywhere like I would in C, C++, javascript, Java, C# and other languages.

Clearly I have either made an error I can't find or there is something in the structure of PHP or Datatables editor that I don't understand.

All help appreciated.

Regards,
Jim

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited August 2017

    It's a scoping issue.
    You have answered your own question, really:

    $log->lwrite("some text");          //<++++++++ this works because $log is instantiated locally
    

    In the previous instance ( preCreate() ) $log is not "visible".

    My own uses of Editor would usually be inside a class:

    class TableToEdit {
    protected $log;          // In your case you might have this.
    
    public function doStuff() {
    $this->log = new OTLogger();  // In your case...
    
    // Now initialise Editor, and you can use $this->log 
    //    in preCreate() and any other function of this class.
    }
    }
    
  • allanallan Posts: 63,299Questions: 1Answers: 10,430 Site admin

    PHP's scoping of variables does fly in the face of all other programming languages for anonymous functions. I'm going to write a tech note up about it since, although it isn't specifically a DataTables issue, is does keep cropping up as a question.

    To use a variable inside an anonymous function, you have to use the use statement (PHP documentation):

    function ( $editor, $values ) use ( $log ) {
    

    Its bonkers if you are used to Javascript or C#, but that's how it works in PHP.

    The other option is to do as @tangerine says and create a new instance in the function.

    Allan

  • obrienjobrienj Posts: 93Questions: 38Answers: 0

    Allan,

    I like your answer and I read the PHP doc, but to show my complete ignorance of this area of PHP, using my example above where exactly do I put the "use"?

    I learned very early in my career (50-some years) that techo-religious debates about programming languages, operating systems, and other such things are pointless. One uses the tool that is available and if that requires a big shift in thinking, so be it , it comes with the territory so please don't mis-understand my comments..

    Regards,
    Jim

  • allanallan Posts: 63,299Questions: 1Answers: 10,430 Site admin
    Answer ✓

    Replace:

    function ( $editor, $values ) {
    

    with

    function ( $editor, $values ) use ( $log ) {
    

    That should be all that is needed.

    Allan

This discussion has been closed.