1. 35
    Defining function return & argument types in PHP
    2m 19s

Defining function return & argument types in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Types are important in any programming language. In PHP, you can define argument & return types to build more reliable applications.

Instructor: [0:00] In order to make sure our code consistently returns the type of data that we expect it to return, we can make sure the data is casted to a specific data type by specifying it as a return type.

[0:13] Let's define a return type for the getPost function, which is the expected return value of a function. If we look at getPost, we can easily see that we are returning an array of data. To make sure this function always returns an array, no matter what, we can specify that as a return type after the parentheses.

[0:36] We can do this by specifying a colon, and then the data type of the returned value that we want to enforce it to. This will be an array.

[0:47] If we then look at getPostText, we can see that our final result is returning a string. Similarly, let's define a return type here with a colon and then followed by the return type that we wish, which here is string.

[1:07] You will want to specify function return types in every function you write to make sure that the function is always returning the data that you expect it to return. This helps enforce the integrity of your code base, so that when you call a function, you can always expect it to return the same data type.

[1:26] For example, you would never want this getPostText function to return an array or some sort of other value, as that would break any code that is calling and displaying the output of this function call.

[1:40] Just like return types, you can also cast arguments to specific types. This should also be done throughout your entire code base. By defining an argument as a specific type, PHP will ensure this value is casted to this type, which will make your code more reliable.

[1:58] We can do this by specifying the type we want arguments to cast to before the name of the argument variable. For example, if we want numPost to always be an integer, we can define that as an ints and followed by a space right before that variable definition.

egghead
egghead
~ 16 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today