Learn how to add an options array as an argument to a function and to send in context parameters that can be used to trigger conditional code.
- [Instructor] In this movie, we will use an array of options, with some of our functions to allow them to change their default behavior. Let me give you an example. Let's say we want to modify the behavior of our find all subjects function, we can pass in an argument to the function and then make the function respond to that argument. A good way to do this is to pass in an array of options instead of a single argument. That way, we can easily add other options by adding to the array, but the argument list does not grow and become hard to manage.
So you can see here, I'm passing in options. I've got a default value, which is an empty array, so it is optional, we don't have to have anything sent in. But if we do, we can pass in an associative array and we can respond to the values that are in that array. You can see here in my code I'm checking the options to see if there is an option for visible. And if there is, I'm setting the variable visible to it. I'm also using the double question mark, that's the new null coalescing operator for PHP 7. It's a handy way to set it to a default value so that if an option for visible is not set, then it will default to being false.
And then, I can just use that inside my code. If visible is true, do one thing. If it's false, do something else. And as I said, we're not limited to just passing in one value, and we're not even limited to passing in Boolean values. You can pass in anything you want and your function can be responsive to that. So here you can see I'm passing in order ascending as well. So presumably my function would take that into account and change its SQL to change the order of the results that were being returned. Notice also that one advantage of using an associative array is that all of our values that are being sent in are clearly labeled.
We know what they are. Visible is what is true, right. If it's just a simple argument I'm passing in, find all subjects, true, how do I know what true is? But by using an associative array, it makes it clear what these values mean. It also means that the order doesn't matter. Doesn't matter if I put order ascending before visible true, those are still values inside the associative array. Clip flexibility is a useful feature. So we just take those options, we pass them into our function, and the results we get back will be modified based on what we pass down.
This is a very handy technique to have in your developer toolbox. Let's try adding it to our project. So let's begin by first going to our public navigation and let's just comment out the skipping that we did in the last movie. Alright, so now there is nothing preventing the visible ones from showing up again. And let's try suppressing the visibility by modifying find all subjects so that it only finds visible subjects. So let's go into our query functions and here's find all subjects. Let's add an option here.
Options and then an empty array. Let's do the checking there to see what values are passed in. Options, we have a value for visible, we'll use it, if not, let's use false. Now we need to modify the behavior based on this option. So what do we want to do? We want to change this SQL, what we want to do is add a where clause to it. So if visible has been set, and is true, then let's add to our SQL. Catenate, and it's going to be where visible equals true and then a space.
Make sure you have the space at the end of it, it's very important that we always are able to concatenate these together and it all still makes sense, right? Whether it's included or not, the SQL still should be able to work. Alright, so now that we've got that, let's save it. Let's go back to our public navigation and we just need to tell find all subjects to use that option. And we do that by just passing in visible is true. Alright, let's go back over to our page, we'll reload, and look at that. We don't see our small business link, right? It's not there anymore.
It's not because we're skipping it, it's because we're never finding it. Now for the pages, we are still seeing the pages, because we haven't done anything about the pages yet. It's only subject we've dealt with so far. So let's go back to our query functions. We want to do the same thing, actually let's grab this first before we head over there. Let's go to the public navigation list. Let's copy this associative array we have and down here where we're finding the pages, we're using find pages by subject ID. Now we're passing in an ID, we want to put the options after that ID. Why is that? Well, because this is actually required.
This is not optional. This is something we have to have. We have to have a subject ID in order for this to work, for find pages by subject ID. This is only for options. Only for things that are optional, ways we want to modify it. Its fundamental behavior requires that we have this first item. So the options come at the end, and they are in fact optional. Alright, so let's save that. Now let's go back and modify that function to work the same way. Let's grab this line, line eight, before we do. I'm just going to copy it. And then I'm going to scroll down to the bottom of this. Down here is where we're going to find that find pages by subject ID.
So I'm just going to paste that line in there, we know we want to make sure that we allow the options. And we're going to check, we're going to set visible. It's going to default to false. Now we just need the modified behavior based on it. Now this one already has a where clause, so it's a little bit different. Before we were adding a where clause in that case. Now we need to modify it so that we have if visible, then SQL is going to add on an and clause where subject ID equals whatever has been provided and visible equals true space.
Make sure the space is at the end again, very important that it always still can be concatenated together correctly, alright? So now we're passing in the option. We're setting it to a default value and we're responding and adjusting the behavior if that option is sent in. Let's try it out. Let's reload our page. Treasury services disappeared. Let's click on about Globe Bank. And sure enough, Globe Bank disappeared. Now about Globe Bank is still showing up, that's because it's actually in a different place here. I'm just going to grab this associative array real quick from here so I don't have to retype it.
We also want to check here on index.php, find pages by subject ID, it needs to also use that same option. Visible true. Save it, reload our page. And sure enough now, the first page is the first visible page. So clicking on a subject doesn't return the first page anymore, it returns the first visible page. And that's the behavior we would expect. Being able to pass in options to your functions is a really useful technique to have.
- Creating a content management system
- Building dynamic content navigation
- Using context and function options for conditional behavior
- Hiding content from the public view
- Insecure direct object reference
- Allowing HTML in dynamic content
- Previewing content in the public context
- Working with cookies and sessions
- Storing status messages in the session
- Secure user authentication (login/logout)
- Regulating page access
- Nesting related resources
- Managing an ordered list automatically