1

May be I am doing something horrible wrong, but still can't find a way. Can anyone have a solution for this ?

I have a interface -

interface L_Item_Action{

    function get_title();

    function get_price();

    function valid( $data );

    function get_error_messages();
}

In this interface I want the function valid() to return boolean values only.

It is not the duplicate of the one referred, becase it was not dealing with interface at all. I have tried boolean function valid( $data ); but that is not working. Is there any way ?

Trying to clear confusion

This thing works --

function sum($a, $b): int {
    return $a + $b;
}

bcoz here I am defining the function. But while defining it as abstract method or interface method, then it doen't work.

function sum($a, $b): int; 
int function sum($a, $b); 

Both of the above is not possible.

5
  • 4
    Possible duplicate of Is it possible to write strictly typed PHP code? Aug 17, 2017 at 13:39
  • Read this. You'll probably need to use strict mode for it to work as you're expecting with booleans. Aug 17, 2017 at 13:44
  • No that is not a duplicate, It was not inside an interface at all
    – Alice
    Aug 17, 2017 at 13:51
  • you do not even tried to check the docs for valid syntax... Aug 17, 2017 at 13:56
  • I am searching for those docs, and whatever I did got, is reading but still no solution. I will keep on trying... but running out of time eventually :(
    – Alice
    Aug 17, 2017 at 14:04

2 Answers 2

2

Update, because of comments.

If you use PHP 7 (which you din't say) you have to use it strict declare(strict_types=1); and use function valid($data): bool{};

Update 2, because of comments

The OP posted her question's code extacly like it is on the documentation for strict types, however that documentation also says it is PHP 7 only, hence the confusion

PHP7-strict-return-types

In PHP 5 you will have to create some sort of observer class that checks your return types through annotation and reflection. But that might weigh very heavy on the code/application. A better way in PHP5 would be do unit testing, to ensure the code is still used the right way.

If however you are trying to force others that use your code, just add a disclaimer ;)

4
  • Thank you for your reply, I have tried that with Abstract Class also, but problem is -- the return type is not working writing the function signature like boolean function valid( $data ); .... Where I am defining functions(inside classes) there , forcing a type works
    – Alice
    Aug 17, 2017 at 14:14
  • You are right, it doesn't work if you only take on board half I said.
    – DarkMukke
    Aug 17, 2017 at 15:31
  • Ohh, now I read about declare(strict_types=1); That's cool info, I am going to upvote you for that. I got the exact idea why it is not working....But unfortunately I was using php 5.....I will upgrade my server and try.....Thanks again
    – Alice
    Aug 18, 2017 at 7:23
  • In PHP 5 it cannot be done, the question code you copied directly from the docs says it is PHP 7 only
    – DarkMukke
    Aug 19, 2017 at 23:56
0

You declare a return type in an interface the same way you declare it in a class.

boolean function valid( $data );

does not work because the return type declaration is in the wrong place. That code will not even parse. The correct syntax is:

function valid( $data ): boolean;

See the PHP documentation for more information and examples.

2
  • 1
    as it turns out, OP is using php 5 (see comments), your answer relies on PHP 7
    – DarkMukke
    Aug 21, 2017 at 12:14
  • 1
    Any answer regarding method return types relies on PHP 7. The OP was using an invalid syntax for declaring the return type, so it wouldn't have worked even if they had the correct PHP version. Aug 21, 2017 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.