Why default method parameters useless?

function test(?int $var = 44444, ?int $var2 = 555555){
 echo $var;
 echo var2;
}

test(); // 44444555555


We want to change only the second parameter, and the first to use the default 444444 method and pass in null, but then it will be used null as a parameter
test(null, 9999); // echo = 9999

In reverse order default settings work as they should, that is.
test(9999); // echo = 9999555555

How to use 1st default parameter?

PS I Have a few solutions, want to learn from others on how much they spike because the Doc says little on this score. By the way, in JS if you pass NaN or undefined, " or '' will apply the default option. Not all test cases for js.
https://www.php.net/manual/ru/functions.arguments.php
April 4th 20 at 00:51
2 answers
April 4th 20 at 00:53
Solution
to pass a value in . the default + use constants

public const DEFAULT_VAR = 4444;
public const DEFAULT_VAR_2 = 55555;

public function test(?int $var = self::DEFAULT_VAR, ?int $var2 = self::DEFAULT_VAR_2){
 echo $var;
 echo var2;
}

...

$obj->test(Obj::DEFAULT_VAR, 9999);

If you just need something complex, then the argument can statkach DTO and ValueObject (depending on context)
public function test(AnyType $anyType) {}

...

$obj->test($valueObject);

whose default value is, and you can adjust as needed

If a complex case is likely to have to split further, for example, one mandatory and the second as a configuration/advanced settings or by some other heuristics. All can be beautiful and laconic. Nullable arguments — for me, bad smell and I prefer a Null Object, of course on the situation.
If we are talking about a Dto, it is possible to make approximately so:
class OrderDto::__construct(ind $id, DateTimeImmutable $createdAt, array $products = []) {}
class UserDto::__construct(ind $id) {}
OrderDetailsDto class::__construct(OrderDto $userDto, ?UserDto $userDto = null) {}


In the case of similar objects, you can:
SettingsDto class::__construct(SettingDto...$settingDto) {}
...
// then use this:
new SettingsDto(
 new SettingDto(1, 'one'),
 new SettingDto(2, 'two'),
...,
 new SettingDto(n, 'n')
)

That is, in a single setting object, you can pass the object-specific settings
In my case the arguments are just as much in the DTO skah once again.
Use CQRS and there is for example command
new CreateUser(

new CreateuserDto(
 $request->request->get('someRequestVariable') , // required
 $request->request->get('someRequestVariable') , // required
 $request->request->get('someRequestVariable') ?? DTO::someLongConstantName, // optional
 $request->request->get('someRequestVariable') ?? DTO::someLongConstantName,
 $request->request->get('someRequestVariable') ?? DTO::someLongConstantName,
 $request->request->get('someRequestVariable') ?? DTO::someLongConstantName,
 $request->request->get('someRequestVariable') ?? DTO::someLongConstantName,
 $request->request->get('someRequestVariable') ?? DTO::someLongConstantName
)

)


Propose to split into
new CreateUserDtoRequired() ,
 new CreateUserDtoOptional()
?
In part, this may solve the situation, only CreateUserDtoOptional will still be created as mentioned previously
$request->request->get('someRequestVariable') ?? DTO::someLongConstantName, // optional
$request->request->get('someRequestVariable') ?? DTO::someLongConstantName,
$request->request->get('someRequestVariable') ?? DTO::someLongConstantName,
$request->request->get('someRequestVariable') ?? DTO::someLongConstantName,
$request->request->get('someRequestVariable') ?? DTO::someLongConstantName,


Which for me, defeats the second, optional DTO. It's as well to test and support it will be necessary. When such an ATT 20-50 is okay, can be, and when they are 200+ more expensive at the time - Brenda commented on April 4th 20 at 00:56
@Brenda,
more than 3 arguments not seen before - Joanne_Von commented on April 4th 20 at 00:59
@Joanne_Von, And how the shape of the edit user settings for example? The creation of heavy objects? Did ads platform, so there 1 is had designer 20+ options and was divided into several stages of the creation of the 1st entity. - Brenda commented on April 4th 20 at 01:02
@Brenda, it is possible an example?

can such a structure come up with:
SettingsDto class::__construct(SettingDto...$settingDto) {}
- Joanne_Von commented on April 4th 20 at 01:05
@Joanne_Von, here's an example from a project. did it 3 years ago
/**
 * @return View
*/
 public function create()
{
 $ad = $this->dispatch(new Adv(
 null, null, null, null, null, null, null,
 null, null, null, null, null, null,
 null, null, null, null, null, null, null,
Adv::DRAFT_YES,
 null, null, null, null, null, null,
 null, null
));
 return redirect()->route('ad.edit', $ad->id());
 }


This all in one command for admin, the user has created hell in several stages via the API. - Brenda commented on April 4th 20 at 01:08
@Brenda, Yes, sad... but I didn't do anything, you need to make the rules design, I have more than 3 never happened, but was nesting
not exactly the same type to collapse to an object? - Joanne_Von commented on April 4th 20 at 01:11
@Brenda,
show the signature of the object RegisterAdvertisementByAdmin - Joanne_Von commented on April 4th 20 at 01:14
@Joanne_Von, bitbucket is falling when you try to open the file(

Having trouble showing that file
Normally, you'd see the file here, but something didn't go right.

- Brenda commented on April 4th 20 at 01:17
@Brenda, how is it? - Joanne_Von commented on April 4th 20 at 01:20
@Joanne_Von, tried using incognito to go without the plugins and cache. Eternal download. It is specifically for that file and other thick file. Had to upload a file

As you can see the code obsolete, no typing in the designer at all. There now seems to 7.4 already did the typing for parameterspublic int $var, I'm still on 7.2 because in 7.3 there is BC. - Brenda commented on April 4th 20 at 01:23
@Brenda, Oh, I don't know how to be that, but it seems to me trivial an associative array here, it'll come :):):):) but it is clear and simple :):):) the keys to write a validator the whole matter, and all

it is possible to divide the objects may part to remove...
perhaps part objects and part of the array (so probably better) — we had the payments are, we beat them so

type so (but I can sooo make a mess of gruppirovku and very wrong to do, the main principle)
new RegisterAdvertisementByAdmin (
 new Condition(
 null, // modelId
 null, // brandId
$conditionId;
),
 new Good( // if the properties are different
[
 'caseMaterialId' => $caseMaterialId,
 'braceletMaterialId' => $braceletMaterialId,
 'braceletColorId' => $braceletColorId,
 ] 
),
 new Owner(

),
 new AdvCredentials(
$deliveryTime,
$issueDateFrom,
$issueDateTo,
),
)


somehow repay the difficulty, even adding objects, somewhere to go to arrays - Joanne_Von commented on April 4th 20 at 01:26
@Brenda, added - Joanne_Von commented on April 4th 20 at 01:29
@Joanne_Von, Yes, I agree with you. Thank you! - Brenda commented on April 4th 20 at 01:32
April 4th 20 at 00:55
Solution
What prevents you to pass an object with parameters already in the code to check for null

var config = {name: 'name', test: 'test', ...};

function testfunc(config){
if(config.name === undefined){
///
}
}
Read the comments to the answer to the question, but now imagine how many will have if conditions to test in the case of Beck, endom. At the front is even worse if you use TS.
All in super typing JS.
b071fa9520544a813f39036cbf5ac028.png
Your approach was thrown back by me a few years ago for Beck end and there is only the DTO and its data type.
For the UI, sometimes using IF, but there are options in 80% 2-3, and methods hundreds of times less if you use frameworks correctly and at maximum. This is of course my thoughts on code style, no strict rules on this subject never was, and never will be the near future.

Here is a table for PHP in strict mode
strict-comparison.png
It is seen that the behavior is much more predictable and audits in times less than for UI, to check if all IF AMI.

And for that matter, it is necessary to check not on what can be and what it should be. That is not a black sheet cecati and waitlist. For example str === undefined replace with str === String - Brenda commented on April 4th 20 at 00:58
@Brenda, at the front is usually used. There is a default configuration, and the constructor is passed your custom configuration where you override what you need.

Here is a typical example
https://github.com/axios/axios/blob/master/lib/def... - Zola.Leffler95 commented on April 4th 20 at 01:01
@Zola.Leffler95, axios not the most reliable or the correct package) - Brenda commented on April 4th 20 at 01:04
@Brenda, Bold statement. But I will not argue, the conversation was about one of the practices of the initialization default values, this is the first thing that came to mind. - Zola.Leffler95 commented on April 4th 20 at 01:07
@Brenda, well... in JS there are no reliable and correct packages))) - Art.Eichma commented on April 4th 20 at 01:10
@Art.Eichma, There are languages in which I write and there are languages which do not contain errors. :)

PS by the number of bad code, PHP competes with VB compared to Delphi.

Don't take that joke to heart in any language is good to write. - Zola.Leffler95 commented on April 4th 20 at 01:13
@Zola.Leffler95, well, it's just your speculation. I'm from an objective point of view are perfect. From popular architectural solutions are only good Ember, but from the point of view of code - TypeORM, which is closer in style to enterprisey PHP/C#/Java solutions.

But bolivarite then you do not want, because you don't know PHP, and therefore not objective. I think You just look at the bindings of the procedures/functions that you have thrown and judged from the standpoint of knowing C# that such tasks are the usual delegation, and not the creation of an amorphous UN-typed anonymous objects. - Art.Eichma commented on April 4th 20 at 01:16
@Zola.Leffler95, that's why we're doing this) - Brenda commented on April 4th 20 at 01:19
@Art.EichmaOf objective historical Netscape was written great, but ie still won. But then all ran over Google.
So not speculation, but the dry figures of capitalization of the companies.
Or another example Wordpress - most popular CMS. But to put it extremely unpleasant. - Zola.Leffler95 commented on April 4th 20 at 01:22
@Zola.Leffler95and Symfony and Laravel are the most popular frames and meddle in them is very nice. So by))) - Art.Eichma commented on April 4th 20 at 01:25

Find more questions by tags PHP