• pass parameter to require/class/construct

    From Arno Welzel@21:1/5 to All on Wed Jan 20 12:54:27 2021
    alex:

    Il 16/01/21 19:42, Arno Welzel ha scritto:
    "require" just includes another file, there is result!

    But it is able to return the value returned by the file

    Yes, I stand corrected:

    <https://www.php.net/manual/en/function.include.php>

    "Handling Returns: include returns FALSE on failure and raises a
    warning. Successful includes, unless overridden by the included file,
    return 1. It is possible to execute a return statement inside an
    included file in order to terminate processing in that file and return
    to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would
    for a normal function"

    Anyway - if you want to have a new class without any parameter in the constructor, you do not write:

    <?php
    return new class {
    function __construct($param) {}
    };

    But just this:

    <?php
    class MyClass {
    function __construct() {}
    };

    And when you want to use that class and return an *instance* of this class:

    <?php
    class MyClass {
    function __construct() {}
    };

    return new MyClass();




    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Arno Welzel@21:1/5 to All on Wed Jan 20 12:56:01 2021
    alex:

    Il 16/01/21 17:38, J.O. Aho ha scritto:
    There is a solution?

    Use the right arguments.

    ????


    <?php
    return new class {
    function __construct($param) {}

    The anomyous class requires one parameter $param.

    Therefore it should be:

    <?php
    return new class('whatever you want') {
    function __construct($param) {}

    Or if you don't want a parameter for the constructor:

    <?php
    return new class() {
    function __construct() {}


    --
    Arno Welzel
    https://arnowelzel.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From alex@21:1/5 to All on Wed Jan 20 14:38:24 2021
    Il 20/01/21 12:54, Arno Welzel ha scritto:
    alex:

    Il 16/01/21 19:42, Arno Welzel ha scritto:
    "require" just includes another file, there is result!

    But it is able to return the value returned by the file

    Yes, I stand corrected:

    <https://www.php.net/manual/en/function.include.php>

    "Handling Returns: include returns FALSE on failure and raises a
    warning. Successful includes, unless overridden by the included file,
    return 1. It is possible to execute a return statement inside an
    included file in order to terminate processing in that file and return
    to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would
    for a normal function"

    Anyway - if you want to have a new class without any parameter in the constructor, you do not write:

    <?php
    return new class {
    function __construct($param) {}
    };

    But just this:

    <?php
    class MyClass {
    function __construct() {}
    };

    And when you want to use that class and return an *instance* of this class:

    <?php
    class MyClass {
    function __construct() {}
    };

    return new MyClass();

    Anyway

    require 'file.php' ($ param1, $ param2, ...)

    it cannot be done, because of course it goes against every paradigm

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)