Chapter 5. Arrays

As we discussed in Chapter 2, PHP supports both scalar and compound data types. In this chapter, we’ll discuss one of the compound types: arrays. An array is a collection of data values organized as an ordered collection of key-value pairs. It may help to think of an array, in loose terms, like an egg carton. Each compartment of an egg carton can hold an egg, but it travels around as one overall container. And, just as an egg carton doesn’t have to contain only eggs (you can put anything in there, like rocks, snowballs, four-leaf clovers, or nuts and bolts), so too an array is not limited to one type of data. It can hold strings, integers, Booleans, and so on. Plus, array compartments can also contain other arrays—but more on that later.

This chapter talks about creating an array, adding and removing elements from an array, and looping over the contents of an array. Because arrays are very common and useful, there are many built-in functions that work with them in PHP. For example, if you want to send email to more than one email address, you’ll store the email addresses in an array and then loop through the array, sending the message to the current email address. Also, if you have a form that permits multiple selections, the items the user selected are returned in an array.

Identifying Elements of an Array

Before we look at creating an array, let’s look at the structure of an existing array. You can access specific values from an existing array using the array variable’s name, followed by the element’s key, or index, within square brackets:

$age['fred']
$shows[2]

The key can be either a string or an integer. String values that are equivalent to integer numbers (without leading zeros) are treated as integers. Thus, $array[3] and $array['3'] reference the same element, but $array['03'] references a different element. Negative numbers are valid keys, but they don’t specify positions from the end of the array as they do in Perl.

You don’t have to quote single-word strings. For instance, $age['fred'] is the same as $age[fred]. However, it’s considered good PHP style to always use quotes, because quoteless keys are indistinguishable from constants. When you use a constant as an unquoted index, PHP uses the value of the constant as the index and emits a warning. This will throw an error in future versions of PHP:

$person = array("name" => 'Peter');
print "Hello, {$person[name]}";
// output: Hello, Peter
// this 'works' but emits this warning as well:
Warning: Use of undefined constant name - assumed 'name' (this will throw an 
Error in a future version of PHP)

You must use quotes if you’re using interpolation to build the array index:

$person = array("name" => 'Peter');
print "Hello, {$person["name"]}";// output: Hello, Peter (with no warning)

Although it’s technically optional, you should also quote the key if you’re interpolating an array lookup to ensure that you get the value you expect. Consider this example:

define('NAME', 'bob');
$person = array("name" => 'Peter');
echo "Hello, {$person['name']}";
echo "<br/>" ;
echo "Hello, NAME";
echo "<br/>" ;
echo NAME ;
// output:
Hello, Peter
Hello, NAME
bob

Storing Data in Arrays

Storing a value in an array will create the array if it doesn’t already exist, but trying to retrieve a value from an array that hasn’t been defined won’t create the array. For example:

// $addresses not defined before this point
echo $addresses[0]; // prints nothing
echo $addresses; // prints nothing

$addresses[0] = "spam@cyberpromo.net";
echo $addresses; // prints "Array"

Using simple assignment to initialize an array in your program can lead to code like this:

$addresses[0] = "spam@cyberpromo.net";
$addresses[1] = "abuse@example.com";
$addresses[2] = "root@example.com";

That’s an indexed array, with integer indices beginning at 0. Here’s an associative array:

$price['gasket'] = 15.29;
$price['wheel'] = 75.25;
$price['tire'] = 50.00;

An easier way to initialize an array is to use the array() construct, which builds an array from its arguments. This builds an indexed array, and the index values (starting at 0) are created automatically:

$addresses = array("spam@cyberpromo.net", "abuse@example.com", 
"root@example.com");

To create an associative array with array(), use the => symbol to separate indices (keys) from values:

$price = array(
 'gasket' => 15.29,
 'wheel' => 75.25,
 'tire' => 50.00
);

Notice the use of whitespace and alignment. We could have bunched up the code, but it wouldn’t have been as easy to read (this is equivalent to the previous code sample), or as easy to add or remove values:

$price = array('gasket' => 15.29, 'wheel' => 75.25, 'tire' => 50.00);

You can also specify an array using a shorter, alternate syntax:

$price = ['gasket' => 15.29, 'wheel' => 75.25, 'tire' => 50.0];

To construct an empty array, pass no arguments to array():

$addresses = array();

You can specify an initial key with => and then a list of values. The values are inserted into the array starting with that key, with subsequent values having sequential keys:

$days = array(1 => "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
// 2 is Tue, 3 is Wed, etc.

If the initial index is a non-numeric string, subsequent indices are integers beginning at 0. Thus, the following code is probably a mistake:

$whoops = array('Fri' => "Black", "Brown", "Green");

// same as
$whoops = array('Fri' => "Black", 0 => "Brown", 1 => "Green");

Extracting Multiple Values

To copy all of an array’s values into variables, use the list() construct:

list ($variable, ...) = $array;

The array’s values are copied into the listed variables in the array’s internal order. By default that’s the order in which they were inserted, but the sort functions described later let you change that. Here’s an example:

$person = array("Fred", 35, "Betty");
list($name, $age, $wife) = $person;
// $name is "Fred", $age is 35, $wife is "Betty"

If you have more values in the array than in the list(), the extra values are ignored:

$person = array("Fred", 35, "Betty");
list($name, $age) = $person; // $name is "Fred", $age is 35

If you have more values in the list() than in the array, the extra values are set to NULL:

$values = array("hello", "world");
list($a, $b, $c) = $values; // $a is "hello", $b is "world", $c is NULL

Two or more consecutive commas in the list() skip values in the array:

$values = range('a', 'e'); // use range to populate the array
list($m, , $n, , $o) = $values; // $m is "a", $n is "c", $o is "e"

Removing and Inserting Elements in an Array

The array_splice() function can remove or insert elements in an array and optionally create another array from the removed elements:

$removed = array_splice(array, start [, length [, replacement ] ]);

We’ll look at array_splice() using this array:

$subjects = array("physics", "chem", "math", "bio", "cs", "drama", "classics");

We can remove the "math", "bio", and "cs" elements by telling array_splice() to start at position 2 and remove 3 elements:

$removed = array_splice($subjects, 2, 3);
// $removed is array("math", "bio", "cs")
// $subjects is array("physics", "chem", "drama", "classics")

If you omit the length, array_splice() removes to the end of the array:

$removed = array_splice($subjects, 2);
// $removed is array("math", "bio", "cs", "drama", "classics")
// $subjects is array("physics", "chem")

If you simply want to delete elements from the source array and you don’t care about retaining their values, you don’t need to store the results of array_splice():

array_splice($subjects, 2);
// $subjects is array("physics", "chem");

To insert elements where others were removed, use the fourth argument:

$new = array("law", "business", "IS");
array_splice($subjects, 4, 3, $new);
// $subjects is array("physics", "chem", "math", "bio", "law", "business", "IS")

The size of the replacement array doesn’t have to be the same as the number of elements you delete. The array grows or shrinks as needed:

$new = array("law", "business", "IS");
array_splice($subjects, 3, 4, $new);
// $subjects is array("physics", "chem", "math", "law", "business", "IS")

To insert new elements into the array while pushing existing elements to the right, delete zero elements:

$subjects = array("physics", "chem", "math');
$new = array("law", "business");
array_splice($subjects, 2, 0, $new);
// $subjects is array("physics", "chem", "law", "business", "math")

Although the examples so far have used an indexed array, array_splice() also works on associative arrays:

$capitals = array(
 'USA' => "Washington",
 'Great Britain' => "London",
 'New Zealand' => "Wellington",
 'Australia' => "Canberra",
 'Italy' => "Rome",
 'Canada' => "Ottawa"
);

$downUnder = array_splice($capitals, 2, 2); // remove New Zealand and Australia
$france = array('France' => "Paris");

array_splice($capitals, 1, 0, $france); // insert France between USA and GB

Converting Between Arrays and Variables

PHP provides two functions, extract() and compact(), that convert between arrays and variables. The names of the variables correspond to keys in the array, and the values of the variables become the values in the array. For instance, this array

$person = array('name' => "Fred", 'age' => 35, 'wife' => "Betty");

can be converted to, or built from, these variables:

$name = "Fred";
$age = 35;
$wife = "Betty";

Creating Variables from an Array

The extract() function automatically creates local variables from an array. The indices of the array elements become the variable names:

extract($person); // $name, $age, and $wife are now set

If a variable created by the extraction has the same name as an existing one, the existing variable’s value is overwritten with the one from the array.

You can modify extract()’s behavior by passing a second argument. The Appendix describes the possible values for this second argument. The most useful value is EXTR_PREFIX_ALL, which indicates that the third argument to extract() is a prefix for the variable names that are created. This helps ensure that you create unique variable names when you use extract(). It is good PHP style to always use EXTR_PREFIX_ALL, as shown here:

$shape = "round";
$array = array('cover' => "bird", 'shape' => "rectangular");

extract($array, EXTR_PREFIX_ALL, "book");
echo "Cover: {$book_cover}, Book Shape: {$book_shape}, Shape: {$shape}";

Cover: bird, Book Shape: rectangular, Shape: round

Traversing Arrays

The most common task with arrays is to do something with every element—for instance, sending mail to each element of an array of addresses, updating each file in an array of filenames, or adding up each element of an array of prices. There are several ways to traverse arrays in PHP, and the one you choose will depend on your data and the task you’re performing.

The Iterator Functions

Every PHP array keeps track of the current element you’re working with; the pointer to the current element is known as the iterator. PHP has functions to set, move, and reset this iterator. The iterator functions are:

current()
Returns the element currently pointed at by the iterator.
reset()
Moves the iterator to the first element in the array and returns it.
next()
Moves the iterator to the next element in the array and returns it.
prev()
Moves the iterator to the previous element in the array and returns it.
end()
Moves the iterator to the last element in the array and returns it.
each()
Returns the key and value of the current element as an array and moves the iterator to the next element in the array.
key()
Returns the key of the current element.

The each() function is used to loop over the elements of an array. It processes elements according to their internal order:

reset($addresses);

while (list($key, $value) = each($addresses)) {
 echo "{$key} is {$value}<br />\n";
}
0 is spam@cyberpromo.net
1 is abuse@example.com

This approach does not make a copy of the array, as foreach does. This is useful for very large arrays when you want to conserve memory.

The iterator functions are useful when you need to consider some parts of the array separately from others. Example 5-1 shows code that builds a table, treating the first index and value in an associative array as table column headings.

Example 5-1. Building a table with the iterator functions
$ages = array(
 'Person' => "Age",
 'Fred' => 35,
 'Barney' => 30,
 'Tigger' => 8,
 'Pooh' => 40
);

// start table and print heading
reset($ages);

list($c1, $c2) = each($ages);

echo("<table>\n<tr><th>{$c1}</th><th>{$c2}</th></tr>\n");

// print the rest of the values
while (list($c1, $c2) = each($ages)) {
 echo("<tr><td>{$c1}</td><td>{$c2}</td></tr>\n");
}

// end the table
echo("</table>");

Calling a Function for Each Array Element

PHP provides a mechanism, array_walk(), for calling a user-defined function once per element in an array:

array_walk(array, callable);

The function you define takes in two or, optionally, three arguments: the first is the element’s value, the second is the element’s key, and the third is a value supplied to array_walk() when it is called. For instance, here’s another way to print table columns made of the values from an array:

$printRow = function ($value, $key)
{
 print("<tr><td>{$key}</td><td>{$value}</td></tr>\n");
};

$person = array('name' => "Fred", 'age' => 35, 'wife' => "Wilma");

echo "<table border=1>";

array_walk($person, $printRow);

echo "</table>";

A variation of this example specifies a background color using the optional third argument to array_walk(). This parameter gives us the flexibility we need to print many tables, with many background colors:

function printRow($value, $key, $color)
{
 echo "<tr>\n<td bgcolor=\"{$color}\">{$value}</td>";
 echo "<td bgcolor=\"{$color}\">{$key}</td>\n</tr>\n";
}

$person = array('name' => "Fred", 'age' => 35, 'wife' => "Wilma");

echo "<table border=\"1\">";

array_walk($person, "printRow", "lightblue");
echo "</table>";

If you have multiple options you want to pass into the called function, simply pass an array in as a third parameter:

$extraData = array('border' => 2, 'color' => "red");
$baseArray = array("Ford", "Chrysler", "Volkswagen", "Honda", "Toyota");

array_walk($baseArray, "walkFunction", $extraData);

function walkFunction($item, $index, $data)
{
 echo "{$item} <- item, then border: {$data['border']}";
 echo " color->{$data['color']}<br />" ;
}
Ford <- item, then border: 2 color->red
Crysler <- item, then border: 2 color->red
VW <- item, then border: 2 color->red
Honda <- item, then border: 2 color->red
Toyota <- item, then border: 2 color->red

The array_walk() function processes elements in their internal order.

Searching for Values

The in_array() function returns true or false, depending on whether the first argument is an element in the array given as the second argument:

if (in_array(to_find, array [, strict])) { ... }

If the optional third argument is true, the types of to_find and the value in the array must match. The default is to not check the data types.

Here’s a simple example:

$addresses = array("spam@cyberpromo.net", "abuse@example.com", 
"root@example.com");
$gotSpam = in_array("spam@cyberpromo.net", $addresses); // $gotSpam is true
$gotMilk = in_array("milk@tucows.com", $addresses); // $gotMilk is false

PHP automatically indexes the values in arrays, so in_array() is generally much faster than a loop checking every value in the array to find the one you want.

Example 5-2 checks whether the user has entered information in all the required fields in a form.

Example 5-2. Searching an array
<?php
function hasRequired($array, $requiredFields) {
 $array =

 $keys = array_keys ( $array );
 foreach ( $requiredFields as $fieldName ) {
 if (! in_array ( $fieldName, $keys )) {
 return false;
 }
 }
 return true;
}
if ($_POST ['submitted']) {
 $testArray = array_filter($_POST);
 echo "<p>You ";
 echo hasRequired ( $testArray, array (
 'name',
 'email_address'
 ) ) ? "did" : "did not";
 echo " have all the required fields.</p>";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
 <p>
 Name: <input type="text" name="name" /><br /> Email address: <input
 type="text" name="email_address" /><br /> Age (optional): <input
 type="text" name="age" />
 </p>
 <p align="center">
 <input type="submit" value="submit" name="submitted" />
 </p>
</form>

A variation on in_array() is the array_search() function. While in_array() returns true if the value is found, array_search() returns the key of the element, if found:

$person = array('name' => "Fred", 'age' => 35, 'wife' => "Wilma");
$k = array_search("Wilma", $person);

echo("Fred's {$k} is Wilma\n");

Fred's wife is Wilma

The array_search() function also takes the optional third strict argument, which requires that the types of the value being searched for and the value in the array match.

Sorting

Sorting changes the internal order of elements in an array and optionally rewrites the keys to reflect this new order. For example, you might use sorting to arrange a list of scores from biggest to smallest, to alphabetize a list of names, or to order a set of users based on how many messages they posted.

PHP provides three ways to sort arrays—sorting by keys, sorting by values without changing the keys, or sorting by values and then changing the keys. Each kind of sort can be done in ascending order, descending order, or an order determined by a user-defined function.

Sorting One Array at a Time

The functions provided by PHP to sort an array are shown in Table 5-1.

Table 5-1. PHP functions for sorting an array
Effect Ascending Descending User-defined order
Sort array by values, then reassign indices starting with 0 sort() rsort() usort()
Sort array by values asort() arsort() uasort()
Sort array by keys ksort() krsort() uksort()

The sort(), rsort(), and usort() functions are designed to work on indexed arrays because they assign new numeric keys to represent the ordering. They’re useful when you need to answer questions such as “What are the top 10 scores?” and “Who’s the third person in alphabetical order?” The other sort functions can be used on indexed arrays, but you’ll only be able to access the sorted ordering by using traversal constructs such as foreach and next().

To sort names into ascending alphabetical order, do something like this:

$names = array("Cath", "Angela", "Brad", "Mira");
sort($names); // $names is now "Angela", "Brad", "Cath", "Mira"

To get them in reverse alphabetical order, simply call rsort() instead of sort().

If you have an associative array that maps usernames to minutes of login time, you can use arsort() to display a table of the top three, as shown here:

$logins = array(
 'njt' => 415,
 'kt' => 492,
 'rl' => 652,
 'jht' => 441,
 'jj' => 441,
 'wt' => 402,
 'hut' => 309,
);

arsort($logins);

$numPrinted = 0;

echo "<table>\n";

foreach ($logins as $user => $time) {
 echo("<tr><td>{$user}</td><td>{$time}</td></tr>\n");

 if (++$numPrinted == 3) {
 break; // stop after three
 }
}

echo "</table>";

If you want that table displayed in ascending order by username, use ksort() instead.

User-defined ordering requires that you provide a function that takes two values and returns a value that specifies the order of the two values in the sorted array. The function should return 1 if the first value is greater than the second, −1 if the first value is less than the second, and 0 if the values are the same for the purposes of your custom sort order.

The program in Example 5-3 applies the various sorting functions to the same data.

Example 5-3. Sorting arrays
<?php
function userSort($a, $b)
{
 // smarts is all-important, so sort it first
 if ($b == "smarts") {
 return 1;
 }
 else if ($a == "smarts") {
 return −1;
 }

 return ($a == $b) ? 0 : (($a < $b) ? −1 : 1);
}

$values = array(
 'name' => "Buzz Lightyear",
 'email_address' => "buzz@starcommand.gal",
 'age' => 32,
 'smarts' => "some"
);

if ($_POST['submitted']) {
 $sortType = $_POST['sort_type'];

 if ($sortType == "usort" || $sortType == "uksort" || $sortType == "uasort") {
 $sortType($values, "userSort");
 }
 else {
 $sortType($values);
 }
} ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post">
 <p>
 <input type="radio" name="sort_type"
 value="sort" checked="checked" /> Standard<br />
 <input type="radio" name="sort_type" value="rsort" /> Reverse<br />
 <input type="radio" name="sort_type" value="usort" /> User-defined<br />
 <input type="radio" name="sort_type" value="ksort" /> Key<br />
 <input type="radio" name="sort_type" value="krsort" /> Reverse key<br />
 <input type="radio" name="sort_type"
 value="uksort" /> User-defined key<br />
 <input type="radio" name="sort_type" value="asort" /> Value<br />
 <input type="radio" name="sort_type"
 value="arsort" /> Reverse value<br />
 <input type="radio" name="sort_type"
 value="uasort" /> User-defined value<br />
 </p>

 <p align="center"><input type="submit" value="Sort" name="submitted" /></p>

 <p>Values <?php echo $_POST['submitted'] ? "sorted by {$sortType}" : "unsorted"; 
 ?>:</p>

 <ul>
 <?php foreach ($values as $key => $value) {
 echo "<li><b>{$key}</b>: {$value}</li>";
 } ?>
 </ul>
</form>

Sorting Multiple Arrays at Once

The array_multisort() function sorts multiple indexed arrays at once:

array_multisort(array1 [, array2, ... ]);

Pass it a series of arrays and sorting orders (identified by the SORT_ASC or SORT_DESC constants), and it reorders the elements of all the arrays, assigning new indices. It is similar to a join operation on a relational database.

Imagine that you have a lot of people, and several pieces of data on each person:

$names = array("Tom", "Dick", "Harriet", "Brenda", "Joe");
$ages = array(25, 35, 29, 35, 35);
$zips = array(80522, '02140', 90210, 64141, 80522);

The first element of each array represents a single record—all the information known about Tom. Similarly, the second element constitutes another record—all the information known about Dick. The array_multisort() function reorders the elements of the arrays, preserving the records. That is, if "Dick" ends up first in the $names array after the sort, the rest of Dick’s information will be first in the other arrays too. (Note that we needed to quote Dick’s zip code to prevent it from being interpreted as an octal constant.)

Here’s how to sort the records first ascending by age, then descending by zip code:

array_multisort($ages, SORT_ASC, $zips, SORT_DESC, $names, SORT_ASC);

We need to include $names in the function call to ensure that Dick’s name stays with his age and zip code. Printing out the data shows the result of the sort:

for ($i = 0; $i < count($names); $i++) {
 echo "{$names[$i]}, {$ages[$i]}, {$zips[$i]}\n";
}
Tom, 25, 80522
Harriet, 29, 90210
Joe, 35, 80522
Brenda, 35, 64141
Dick, 35, 02140

Acting on Entire Arrays

PHP has several useful built-in functions for modifying or applying an operation to all elements of an array. You can calculate the sum of an array, merge multiple arrays, find the difference between two arrays, and more.

Using Arrays to Implement Data Types

Arrays crop up in almost every PHP program. In addition to their obvious purpose of storing collections of values, they’re also used to implement various abstract data types. In this section, we show how to use arrays to implement sets and stacks.

Stacks

Although not as common in PHP programs as in other programs, one fairly common data type is the last-in first-out (LIFO) stack. We can create stacks using a pair of PHP functions, array_push() and array_pop(). The array_push() function is identical to an assignment to $array[]. We use array_push() because it accentuates the fact that we’re working with stacks, and the parallelism with array_pop() makes our code easier to read. There are also array_shift() and array_unshift() functions for treating an array like a queue.

Stacks are particularly useful for maintaining state. Example 5-4 provides a simple state debugger that allows you to print out a list of which functions have been called up to this point (i.e., the stack trace).

Example 5-4. State debugger
$callTrace = array();

function enterFunction($name)
{
 global $callTrace;
 $callTrace[] = $name;

 echo "Entering {$name} (stack is now: " . join(' -> ', $callTrace) . ")<br />";
}

function exitFunction()
{
 echo "Exiting<br />";

 global $callTrace;
 array_pop($callTrace);
}

function first()
{
 enterFunction("first");
 exitFunction();
}

function second()
{
 enterFunction("second");
 first();
 exitFunction();
}

function third()
{
 enterFunction("third");
 second();
 first();
 exitFunction();
}

first();
third();

Here’s the output from Example 5-4:

Entering first (stack is now: first)
Exiting
Entering third (stack is now: third)
Entering second (stack is now: third -> second)
Entering first (stack is now: third -> second -> first)
Exiting
Exiting
Entering first (stack is now: third -> first)
Exiting
Exiting

Implementing the Iterator Interface

Using the foreach construct, you can iterate not only over arrays, but also over instances of classes that implement the Iterator interface (see Chapter 6 for more information on objects and interfaces). To implement the Iterator interface, you must implement five methods on your class:

current()
Returns the element currently pointed at by the iterator.
key()
Returns the key for the element currently pointed at by the iterator.
next()
Moves the iterator to the next element in the object and returns it.
rewind()
Moves the iterator to the first element in the array.
valid()
Returns true if the iterator currently points at a valid element, and false otherwise.

Example 5-5 reimplements a simple iterator class containing a static array of data.

Example 5-5. Iterator interface
class BasicArray implements Iterator
{
 private $position = 0;
 private $array = ["first", "second", "third"];

 public function __construct()
 {
 $this->position = 0;
 }

 public function rewind()
 {
 $this->position = 0;
 }

 public function current()
 {
 return $this->array[$this->position];
 }

 public function key()
 {
 return $this->position;
 }

 public function next()
 {
 $this->position += 1;
 }

 public function valid()
 {
 return isset($this->array[$this->position]);
 }
}

$basicArray = new BasicArray;

foreach ($basicArray as $value) {
 echo "{$value}\n";
}

foreach ($basicArray as $key => $value) {
 echo "{$key} => {$value}\n";
}

first
second
third
 
0 => first
1 => second
2 => third

When you implement the Iterator interface on a class, it allows you only to traverse elements in instances of that class using the foreach construct; it does not allow you to treat those instances as arrays or parameters to other methods. This, for example, rewinds the Iterator pointing at $trie’s properties using the built-in rewind() function instead of calling the rewind() method on $trie:

class Trie implements Iterator
{
 const POSITION_LEFT = "left";
 const POSITION_THIS = "this";
 const POSITION_RIGHT = "right";

 var $leftNode;
 var $rightNode;

 var $position;

 // implement Iterator methods here...
}

$trie = new Trie();

rewind($trie);

The optional SPL library provides a wide variety of useful iterators, including filesystem directory, tree, and regex matching iterators.

What’s Next

The last three chapters—on functions, strings, and arrays—have covered a lot of foundational ground. The next chapter builds on this foundation and takes you into the newish world of objects and object-oriented programming (OOP). Some argue that OOP is the better way to program, as it is more encapsulated and reusable than procedural programming. That debate continues, but once you get into the object-oriented approach to programming and understand its benefits, you can make an informed decision about how you’ll program in the future. That said, the overall trend in the programming world is to use OOP as much as possible.

One word of caution before you continue: there are many situations where a novice OOP programmer can get lost, so be sure you’re really comfortable with OOP before you do anything major or mission-critical with it.