Class 15: PHP Part 2
Instructions:
- Study the additional videos sections below
- Review the PHP Lab instructions
- Wait for me to provide guidance/feedback on your project if I haven't done so already
Introduction to PHP Files
Introductory video on PHP Files (opens in a new tab)
Like I mentioned previously, the PHP Documentation is well worth exploring (opens in a new tab)
If you prefer to go from file to a string, you can use file_get_contents
(opens in a new tab).
You should be able to use explode
(opens in a new tab)
and/or trim
(opens in a new tab) to get an array of lines as well.
Working with Files in PHP
Here's a video on working with files in PHP (opens in a new tab)
Here are links to the docs for some of the filesystem functions we explore in the video:
scandir
(opens in a new tab)glob
(opens in a new tab)chmod
(opens in a new tab)chgrp
(opens in a new tab)file_put_contents
(opens in a new tab)
Also, while we didn't go over many of these in the video in-depth (or at all), here are a few more popular filesystem functions that I would encourage you to explore
file_exists
(opens in a new tab)dirname
(opens in a new tab)is_dir
(opens in a new tab)realpath
(opens in a new tab)getcwd
(opens in a new tab)mkdir
(opens in a new tab)
The search bar in the upper right of the PHP site (opens in a new tab) however, should be the preferred way of looking up functions.
PHP Functions
Here's the video that introduces writing functions in PHP (opens in a new tab)
PHP provides a great overview on functions (opens in a new tab) that covers all different common aspects of functions, including:
- how to make functions (opens in a new tab)
- how to create arguments (opens in a new tab)
- how to return values (opens in a new tab)
As a reference, here is the sample code for a function that we went over in the video:
function bmi($weightLbs, $heightIn) {
return 703 * $weightLbs / ($heightIn * $heightIn);
}
PHP Classes
Here is a shorter video introducing classes in PHP (opens in a new tab)
Just like with functions, the PHP Manual has a large list of articles related to classes (opens in a new tab)
The class structure itself resembles C++ and Java, having:
- access modifiers (
public
/private
) for both variables and methods. - constructors (via the
__construct
method) - default values (see the
= 0
insetX
/setY
) - instance variables
x
/y
(accessed using the$this
keyword)
class Point {
private $x;
private $y;
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
public function getX() {
return $this->x;
}
public function getY() {
return $this->y;
}
public function setX($newX) {
$this->x = $newX;
}
public function setY($newY) {
$this->y = $newY;
}
}
Using PHPStorm and Debugging
In this screensharing video (opens in a new tab), I go over trying to implement a distance method and then debug it when things don't go according to plan.
I also showed you
var_dump
(opens in a new tab)
We talked about using the Key Promoter X plugin (opens in a new tab) to learn more of the keyboard shortcuts that we are often missing. It's optional to install, but serves as a handy reminder to learn more of those shortcuts. While I understand that for many of you it does not seem that it would be such a significant time savings. However, once you start coding enough and you start chaining those shortcuts together it can save time.
In addition, to help, I would not learn multiple sets of keyboard shortcuts. All IDEs allow you to import your own custom set of shortcuts.
If you stick to just one of these programmer environment camps:
- Eclipse
- VSCode
- JetBrains
- VIM
- Emacs
You should be able to find a library to help you support what you are doing as you move around and use different editors.
Also, the Debugger is an extremely powerful tool.
Use it!
Don't just rely on print
statements.
Print statements are useful, but they are a headache as you have to keep running and re-running the program.
Spend the time to learn to use the debugger properly and it can help you get faster feedback in the long run.
Advanced Topics
Lastly we go over a number of topics in out last video (opens in a new tab) including:
- Inheritance
- Static classes and methods
- HTTP Requests
- Embedding PHP Wisely
- PHP Short Tags
Here is our previous point class but with a few additional modifications:
const
andstatic
fields and methods, (accessed using theself
keyword)
Also keep in mind that while the video says that PHP was not typed, that is historically true. However, with PHP 7, types were fully supported. See this example that shows those types embedded in as well as some additional features.
class Point {
private int $x;
private int $y;
const DEFAULT_VALUE = 0;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function getX() {
return $this->x;
}
public function getY() {
return $this->y;
}
public function setX(int $newX = self::DEFAULT_VALUE) {
$this->x = $newX;
}
public function setY(int $newY = self::DEFAULT_VALUE) {
$this->y = $newY;
}
}