New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Refactoring the navigation

From: PHP with MySQL Essential Training

Video: Refactoring the navigation

Now that we have a navigation that has a list of our subjects and a list of our pages, I'd like us to take a moment to talk about refactoring that navigation. Refactoring is a programming concept that I think it's important to introduce you to. The definition of code refactoring is, revising existing code to change its structure or its appearance but without changing its behavior, It's going to do the exact same thing. We're just going to revise it in place. It's essentially a fancy word for saying let's improve our code. We don't just revise our code for no reason. We need to have specific goals in mind when we refactor, and you can also think of these goals as the criteria for deciding if your revised code is an improvement over the old version or not. So, what are those goals? Well, we revise code for simplicity, for clarity, for maintainability, for efficiency, and for flexibility. And under flexibility, I would say that come in two categories. That's reusability, so that we're flexible in how we use it. And also extensibility, that it can be extended to do more things. So, these are our goals, this is what we want to do with our code to make it better.

Refactoring the navigation

Now that we have a navigation that has a list of our subjects and a list of our pages, I'd like us to take a moment to talk about refactoring that navigation. Refactoring is a programming concept that I think it's important to introduce you to. The definition of code refactoring is, revising existing code to change its structure or its appearance but without changing its behavior, It's going to do the exact same thing. We're just going to revise it in place. It's essentially a fancy word for saying let's improve our code. We don't just revise our code for no reason. We need to have specific goals in mind when we refactor, and you can also think of these goals as the criteria for deciding if your revised code is an improvement over the old version or not. So, what are those goals? Well, we revise code for simplicity, for clarity, for maintainability, for efficiency, and for flexibility. And under flexibility, I would say that come in two categories. That's reusability, so that we're flexible in how we use it. And also extensibility, that it can be extended to do more things. So, these are our goals, this is what we want to do with our code to make it better.

And if we can make changes to our code so it gives the same results, but yet it's more simple or more maintainable, then that's better code. That's going to be better for us in the long run. So, let's look at our navigation and see how we can refactor it. So, let's open up manage_content, and I just want to note here, we did a touch of refactoring earlier when we did confirm_query. You'll remember that we took confirm_query, we moved it into our functions, so that we didn't have to repeat ourselves all the time. We could have that each and every time, and it makes it more maintainable because we can come back here and make changes to how we confirm queries.

And we can just do it in the function, and then it'll trickle out to the rest of our pages. Functions are often great ways to refactor code, because the code does the same tasks but in a different location that's out of our way. It becomes reusable and it also gets a function name that helps clarify the codes purpose. I don't know about you, but I find confirmed query to be a lot easier to read and understand what it does than to look at this code here. If not result set die. This makes it really clear what I'm trying to do, what my purpose is, so the code's more maintainable and it's more flexible.

So, let's look now at this list here we've got where we're finding all of these subjects. Let's take that and put that into a function. A function that we can call, and then it will return a result set to us. So, let's start a function over here and let's call this one function find all subjects. We go plural, and I'm just going to grab everything that was in here. I don't need those PHP tags. I'm just going to cut. I'm already inside PHP. I'll paste it right in there. So now, find_all_subjects, we'll assemble a query it will perform the query on a connection and then, confirm the query on the subject set.

So, we need to do a couple things different when we're working with functions. We need to return a value, let's return the subject set. And, notice that we have connection here. So, we need to either pass this value in as an argument to find all subjects, or we can just call global on it here. So, getting the global connection, bringing it in from the global scope so that we can use it. And this is a perfectly appropriate place to use global. To grab that connection because we're just going to use it in our query and then we're done with it. We're not going to try to change the values or anything like that.

So, now we have find all subjects, that's a nice little function. So, over here instead of having all that code before, we can not just say subject set equal find all subjects, see how that works? Now I think that's a good refactor. It took all of this SQL that we were working with here and it got it out of our way. We don't need to be worried about it. What we care about is the fact that we're finding all subjects. So let's do that. Let's just label it, find all subjects. And you want to know what this does? It finds all subjects and it assigns them to subject set. Then they're available to me. Let's do the same thing for the pages.

You've got all of this page query right here. Let's just take that same thing out, we just copy it real quick, actually I'll cut it and let's jump over here. And let's make ourselves a new function, function find all pages and then let's paste that in. Now, wait a minute. We're not actually trying to find all pages. That's not what we really want to do. What we really want to do is find pages for a subject. Find pages for subject, and we need to provide the subject that we're looking for. We have subject id that was passed in before.

Well, we don't want to use this, we don't want to look for the key inside of an array. Instead, let's actually just pass in subject id, so we're going to expect an integer to come in here. Then we'll need our global connection, just like we had before. Now instead of subject id, this is going to be subject id. We've got our connection. We confirm our page set. And then the very last thing of course is return that page set to us. Okay. Let's save it. Now let's come back over here. I'm just going to copy this. And let's paste it in here.

And we're going to set page set equal to it. And notice, this page set has nothing to do with the value that (INAUDIBLE) here. This is a local variable that only has scope inside the function. This is one that I'm defining in the global scope, so it's fine to reuse it. Now, I can't, though, call it subject id here. I need to actually put in the value for that. And we had that before, that was inside the associative array for the subject. Bring up its id. So there, find pages for this subject with this subject id. And guess what, it returns a page set to me, but then I'm ready to iterate through.

See how much slimmer this is now? Look at my page, it's much, much cleaner, it's much clearer what it's doing. I think that's a good refactor. Of course, for a refactor to work, it has to do the same thing, without breaking anything. It has to have the same functionality. So, let's try it, let's see. Let's go back here to manage content, let's reload the page. And sure enough, it does the exact same thing, there's no difference in what it does. And yet, now, I have some reusable functions over here. Whenever I want to find all subjects, I have a function that does that for me. Whenever I want to find the pages that belong to a subject, I have a function that does that for me. That's the basic idea behind refactoring.

We're going to be doing refactoring a lot throughout the rest of this tutorial. So, when you use that term just know that we're going to be changing our code to make it better. To improve it with the goals in mind of simplicity, clarity, maintainability, efficiency and flexibility.

Show transcript

This video is part of

Image for PHP with MySQL Essential Training
PHP with MySQL Essential Training

131 video lessons · 31709 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 4m 8s
    1. Welcome
      1m 0s
    2. Using the exercise files
      3m 8s
  2. 15m 6s
    1. What is PHP?
      3m 52s
    2. The history of PHP
      2m 51s
    3. Why choose PHP?
      4m 10s
    4. Installation overview
      4m 13s
  3. 54m 53s
    1. Overview
      2m 33s
    2. Working with Apache Web Server
      6m 56s
    3. Changing the document root
      7m 24s
    4. Enabling PHP
      6m 16s
    5. Upgrading PHP
      3m 30s
    6. Configuring PHP
      10m 3s
    7. Installing MySQL
      5m 46s
    8. Configuring MySQL
      7m 24s
    9. Text editor
      5m 1s
  4. 31m 25s
    1. Overview
      3m 27s
    2. Installing WampServer
      5m 46s
    3. Finding the document root
      2m 24s
    4. Configuring PHP
      8m 12s
    5. Configuring MySQL
      5m 45s
    6. Text editor
      5m 51s
  5. 19m 12s
    1. Embedding PHP code on a page
      6m 43s
    2. Outputting dynamic text
      5m 55s
    3. The operational trail
      2m 27s
    4. Inserting code comments
      4m 7s
  6. 1h 18m
    1. Variables
      7m 50s
    2. Strings
      4m 38s
    3. String functions
      8m 54s
    4. Numbers part one: Integers
      6m 27s
    5. Numbers part two: Floating points
      5m 25s
    6. Arrays
      10m 0s
    7. Associative arrays
      6m 37s
    8. Array functions
      6m 33s
    9. Booleans
      3m 50s
    10. NULL and empty
      5m 15s
    11. Type juggling and casting
      8m 27s
    12. Constants
      4m 43s
  7. 27m 37s
    1. If statements
      6m 0s
    2. Else and elseif statements
      4m 16s
    3. Logical operators
      7m 30s
    4. Switch statements
      9m 51s
  8. 42m 15s
    1. While loops
      8m 41s
    2. For loops
      5m 59s
    3. Foreach loops
      8m 16s
    4. Continue
      8m 28s
    5. Break
      4m 8s
    6. Understanding array pointers
      6m 43s
  9. 37m 25s
    1. Defining functions
      8m 25s
    2. Function arguments
      5m 32s
    3. Returning values from a function
      7m 33s
    4. Multiple return values
      4m 53s
    5. Scope and global variables
      6m 2s
    6. Setting default argument values
      5m 0s
  10. 20m 18s
    1. Common problems
      3m 47s
    2. Warnings and errors
      8m 36s
    3. Debugging and troubleshooting
      7m 55s
  11. 57m 57s
    1. Links and URLs
      5m 33s
    2. Using GET values
      5m 35s
    3. Encoding GET values
      8m 41s
    4. Encoding for HTML
      9m 26s
    5. Including and requiring files
      7m 40s
    6. Modifying headers
      6m 45s
    7. Page redirection
      6m 43s
    8. Output buffering
      7m 34s
  12. 1h 3m
    1. Building forms
      7m 28s
    2. Detecting form submissions
      5m 59s
    3. Single-page form processing
      7m 57s
    4. Validating form values
      10m 40s
    5. Problems with validation logic
      9m 54s
    6. Displaying validation errors
      7m 23s
    7. Custom validation functions
      6m 28s
    8. Single-page form with validations
      7m 25s
  13. 28m 5s
    1. Working with cookies
      2m 49s
    2. Setting cookie values
      5m 55s
    3. Reading cookie values
      6m 1s
    4. Unsetting cookie values
      4m 51s
    5. Working with sessions
      8m 29s
  14. 48m 39s
    1. MySQL introduction
      6m 43s
    2. Creating a database
      7m 41s
    3. Creating a database table
      7m 42s
    4. CRUD in MySQL
      5m 48s
    5. Populating a MySQL database
      7m 32s
    6. Relational database tables
      6m 40s
    7. Populating the relational table
      6m 33s
  15. 56m 4s
    1. Database APIs in PHP
      4m 51s
    2. Connecting to MySQL with PHP
      7m 45s
    3. Retrieving data from MySQL
      8m 47s
    4. Working with retrieved data
      6m 12s
    5. Creating records with PHP
      6m 58s
    6. Updating and deleting records with PHP
      9m 6s
    7. SQL injection
      3m 5s
    8. Escaping strings for MySQL
      6m 45s
    9. Introducing prepared statements
      2m 35s
  16. 35m 58s
    1. Blueprinting the application
      7m 19s
    2. Building the CMS database
      5m 14s
    3. Establishing your work area
      4m 38s
    4. Creating and styling the first page
      4m 22s
    5. Making page assets reusable
      6m 36s
    6. Connecting the application to the database
      7m 49s
  17. 32m 49s
    1. Adding pages to the navigation subjects
      5m 58s
    2. Refactoring the navigation
      6m 7s
    3. Selecting pages from the navigation
      6m 2s
    4. Highlighting the current page
      5m 26s
    5. Moving the navigation to a function
      9m 16s
  18. 1h 45m
    1. Finding a subject in the database
      9m 48s
    2. Refactoring the page selection
      10m 52s
    3. Creating a new subject form
      6m 55s
    4. Processing form values and adding subjects
      11m 20s
    5. Passing data in the session
      9m 16s
    6. Validating form values
      9m 40s
    7. Creating an edit subject form
      8m 30s
    8. Using single-page submission
      7m 44s
    9. Deleting a subject
      9m 44s
    10. Cleaning up
      10m 37s
    11. Assignment: Pages CRUD
      4m 30s
    12. Assignment results: Pages CRUD
      6m 10s
  19. 39m 26s
    1. The public appearance
      8m 52s
    2. Using a context for conditional code
      11m 37s
    3. Adding a default subject behavior
      6m 9s
    4. The public content area
      5m 51s
    5. Protecting page visibility
      6m 57s
  20. 1h 3m
    1. User authentication overview
      4m 3s
    2. Admin CRUD
      8m 41s
    3. Encrypting passwords
      7m 26s
    4. Salting passwords
      5m 42s
    5. Adding password encryption to CMS
      11m 54s
    6. New PHP password functions
      3m 13s
    7. Creating a login system
      11m 28s
    8. Checking for authorization
      5m 48s
    9. Creating a logout page
      5m 40s
  21. 2m 4s
    1. Next steps
      2m 4s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.