Newbie having trouble with first steps

Newbie having trouble with first steps

davestricklerdavestrickler Posts: 2Questions: 1Answers: 0

While I'm a competent PHP programmer from the 5.x versions, but I'm clearly missing something here. I've followed the examples, setup (https://editor.datatables.net/manual/php/getting-started) by connecting to my database, and am running the following PHP:

// Running PHP Version 5.5.9-1ubuntu4.22
include("vendor/datatables/editor/php/DataTables.php");
use DataTables\Editor;
$editor = Editor::inst($db, 'system_logs', 'id');
Editor::inst( $db, 'system_logs', 'id' )
    ->fields(
        Field::inst( 'user_uuid' )
    );

When I run this, PHP errors with Fatal error: Class 'Field' not found in .../data_browser.php on line 7
I would have assumed that Field would be defined just like Editor, but I'm used to the OO model of $var = editor->function;, so I may be missing something obvious.

-- Dave

Answers

  • davestricklerdavestrickler Posts: 2Questions: 1Answers: 0

    Update, I am getting it to work (I think) with this syntax:

    $editor->fields(Editor\Field::inst('user_uuid'))
    

    But I still can't explain why the example causes an error. Does 'Field' need to be prefixed as 'Editor\Field'?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    At the link you posted, find this:

    Now the complete example is:

    which shows the use of PHP namespaces.

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    Yes, Editor's PHP libraries use namespaces to ensure code separation. You can either specify a fully qualified name space as you have above (Editor\Field, Editor\Join, etc) or your can make use of PHP's use statement to allow it to be shorten like in the Editor example.

    If you have a look at the Editor PHP file examples you'll find:

    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    

    at the top of all of them just to make the code a little easier to read (full namespaces everywhere doesn't make for attractive code).

    SitePoint have a good tutorial on namespaces.

    Allan

This discussion has been closed.