1

Question

In the PHP Official Documentation I found somewhere declarations like this:

public int save ( string $filename [, int $options ] )

and like this:

public mixed load ( string $filename [, int $options = 0 ] )

What's the difference between both, on the $options argument?

The second one I'm sure that has the $options argument set to 0 (when I don't specify it). But the first one? Isn't it saying that the method has 2 mandatory arguments? (so whats the square brackets for?).

Example

I'm trying to override this:

public bool schemaValidate ( string $filename [, int $flags ] )

(fingerprint pasted from schemaValidate() PHP documentation)

but if I declare the second argument $flags, then I get a

SchemaValidate() should be compatible with DOMDocument::schemaValidate($filename)

If I remove the declaration of $flags everything works (like if I were in < PHP 5.5.2)

While I run PHP 5.5.9 and $flags has been introduced in schemaValidate() constructor from PHP 5.5.2, why do I get the incompatibly problem?

I don't get if the PHP Documentation is trustable or if I should check somewhere else for the right function fingerprint, when I want to override native methods.

I tried to have a look at the source code, but the function is merely an alias to a C function.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
Kamafeather
  • 8,663
  • 14
  • 69
  • 99

1 Answers1

3
public int save ( string $filename [, int $options ] )

means that the $options is optional, if you do not pass $options, its default value is null

public mixed load ( string $filename [, int $options = 0 ] )

means that the $options is optional, if you do not pass $options, its default value is 0

To achieve the first documentation, they must have made a function and override them by themselves, as follows:

public int save ( string $filename ){
    //do some stuff...
}

//later in code...

public int save ( string $filename, int $options ){
    //do some stuff...
}

While in the second one, they just did something like:

public mixed load ( string $filename, int $options = 0 ){
    //do some stuff...
}
DJ'
  • 1,760
  • 1
  • 13
  • 26
  • It's what I thought. But then I asked myself: _since when PHP can declare an optional parameter without specifying a default value?_ How can I do it? – Kamafeather Mar 19 '15 at 15:41
  • simply by overriding the function. See my edited answer – DJ' Mar 19 '15 at 15:42
  • 1
    Ok, your updates make sense and probably is an already overridden function. But what if I want to define a _new_ function that has an **optional parameter**, but without specifying its default value? -- `function foo($bar)`, if called like `echo foo()` will tell me that it expects 1 argument. If I do `function foo($bar = 0)` and then `echo foo()` it will work. Is it possible to get the case where the call will just pass `null` as the parameter value (talking outside of the _overriding_ context)? – Kamafeather Mar 19 '15 at 15:48
  • Moreover I checked the PHP github repository and the only declaration of `schemaValidate` is in that alias (see link in my question), and I don't see any function overriding. (but Github code search is not working always good, so could be that I'm missing some code) – Kamafeather Mar 19 '15 at 15:52
  • 1
    Sorry for late reply, I got busy that night and forgot about the question. Actually, you can achieve what you need by two ways, make a function `foo($bar)`, override it by `foo()`, and inside `foo(){ foo(null); }` ... simple :) ... to make it simpler, PHP has `foo($bar = null)` – DJ' Mar 23 '15 at 09:40
  • Ok, no real native straight way to do it. Thanks for answering! – Kamafeather Mar 23 '15 at 21:09
  • ummm ... this is the exact way how it works in PHP, and this is native. If you are referring something similar to `params` keyword in C#, nop you wont find that in PHP – DJ' Mar 24 '15 at 09:09
  • No no I don't expect C# :) The truth is that I was excepting the opposite: that was not possible at all. For that I was quite surprised discovering this possibility with overriding (while I was excepting a _Fatal Error_). – Kamafeather Mar 24 '15 at 11:34