Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18524 > unrolled thread

pass parameter to require/class/construct

Started byalex <1j9448a02@lnx159sneakemail.com.invalid>
First post2021-01-16 09:43 +0100
Last post2021-01-20 14:38 +0100
Articles 10 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  pass parameter to require/class/construct alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-01-16 09:43 +0100
    Re: pass parameter to require/class/construct John-Paul Stewart <jpstewart@personalprojects.net> - 2021-01-16 11:06 -0500
    Re: pass parameter to require/class/construct "J.O. Aho" <user@example.net> - 2021-01-16 17:38 +0100
      Re: pass parameter to require/class/construct alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-01-18 15:24 +0100
        Re: pass parameter to require/class/construct "J.O. Aho" <user@example.net> - 2021-01-18 17:18 +0100
        Re: pass parameter to require/class/construct Arno Welzel <usenet@arnowelzel.de> - 2021-01-20 12:56 +0100
    Re: pass parameter to require/class/construct Arno Welzel <usenet@arnowelzel.de> - 2021-01-16 19:42 +0100
      Re: pass parameter to require/class/construct alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-01-18 15:36 +0100
        Re: pass parameter to require/class/construct Arno Welzel <usenet@arnowelzel.de> - 2021-01-20 12:54 +0100
          Re: pass parameter to require/class/construct alex <1j9448a02@lnx159sneakemail.com.invalid> - 2021-01-20 14:38 +0100

#18524 — pass parameter to require/class/construct

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-01-16 09:43 +0100
Subjectpass parameter to require/class/construct
Message-ID<rtu901$voh$1@gioia.aioe.org>
$ cat object.php
<?php
$obj = require 'class.php';

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

$ php object.php
Fatal error: Uncaught ArgumentCountError: Too few arguments to function 
class@anonymous::__construct(), 0 passed in class.php on line 2 and 
exactly 1 expected in class.php on line 3

It's obvious.

I tried this:

$obj = require 'class.php'(123);

but it does not work: Fatal error: Uncaught Error: Call to undefined 
function class.php() in object.php on line 2

There is a solution?

[toc] | [next] | [standalone]


#18525

FromJohn-Paul Stewart <jpstewart@personalprojects.net>
Date2021-01-16 11:06 -0500
Message-ID<i6gh8fFefqcU1@mid.individual.net>
In reply to#18524
On 2021-01-16 3:43 a.m., alex wrote:
> $ cat object.php
> <?php
> $obj = require 'class.php';

That's not the right syntax for require nor for creating an object.
Those are two different steps.  So the code should be:

<?php
require 'class.php';
$obj = new class(123);

First you use require to include the code from class.php, then you
create an object (passing parameters as necessary).

[toc] | [prev] | [next] | [standalone]


#18526

From"J.O. Aho" <user@example.net>
Date2021-01-16 17:38 +0100
Message-ID<i6gj3cFerqrU1@mid.individual.net>
In reply to#18524
On 16/01/2021 09.43, alex wrote:
> $ cat object.php
> <?php
> $obj = require 'class.php';
> 
> $ cat class.php
> <?php
> return new class {
>      function __construct($param) {}
> };
> 
> $ php object.php
> Fatal error: Uncaught ArgumentCountError: Too few arguments to function 
> class@anonymous::__construct(), 0 passed in class.php on line 2 and 
> exactly 1 expected in class.php on line 3
> 
> It's obvious.

Yes, you have the wrong number of arguments when you create the 
anonymous class.


> There is a solution?

Use the right arguments.

-- 

  //Aho

[toc] | [prev] | [next] | [standalone]


#18531

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-01-18 15:24 +0100
Message-ID<ru45mg$1h08$1@gioia.aioe.org>
In reply to#18526
Il 16/01/21 17:38, J.O. Aho ha scritto:
>> There is a solution?
> 
> Use the right arguments.

????

[toc] | [prev] | [next] | [standalone]


#18533

From"J.O. Aho" <user@example.net>
Date2021-01-18 17:18 +0100
Message-ID<i6lqn4FfdmiU1@mid.individual.net>
In reply to#18531
On 18/01/2021 15.24, alex wrote:
> Il 16/01/21 17:38, J.O. Aho ha scritto:
>>> There is a solution?
>>
>> Use the right arguments.
> 
> ????

  1 != 0

[toc] | [prev] | [next] | [standalone]


#18535

FromArno Welzel <usenet@arnowelzel.de>
Date2021-01-20 12:56 +0100
Message-ID<i6qk2gFddjrU2@mid.individual.net>
In reply to#18531
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

[toc] | [prev] | [next] | [standalone]


#18529

FromArno Welzel <usenet@arnowelzel.de>
Date2021-01-16 19:42 +0100
Message-ID<i6gqcfFg1noU3@mid.individual.net>
In reply to#18524
alex:

> $ cat object.php
> <?php
> $obj = require 'class.php';

This makes no sense.

"require" just includes another file, there is result! Also see here:

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

Please learn how PHP works first and then ask questions. And next time
first consult the official documentation before asking other people to
do your homework!


-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#18532

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-01-18 15:36 +0100
Message-ID<ru46dv$2kb$1@gioia.aioe.org>
In reply to#18529
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

<?php // 1.php
var_dump(require '2.php');

<?php // 2.php
return true;

Output of 1.php:

bool(true)



[toc] | [prev] | [next] | [standalone]


#18534

FromArno Welzel <usenet@arnowelzel.de>
Date2021-01-20 12:54 +0100
Message-ID<i6qjviFddjrU1@mid.individual.net>
In reply to#18532
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

[toc] | [prev] | [next] | [standalone]


#18536

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2021-01-20 14:38 +0100
Message-ID<ru9bof$1g6p$1@gioia.aioe.org>
In reply to#18534
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web