Skip to content

Latest commit

 

History

History
335 lines (299 loc) · 8.64 KB

php-cert-apr-2022.md

File metadata and controls

335 lines (299 loc) · 8.64 KB

PHP Certification - April 2022

TODO

  • Q: Which extensions are enabled by default?

  • A: TBD

  • Q: In what PHP version was SoapClient::__call() deprecated?

  • Q: What is the special syntax needed to use time() in DateTime() constructor?

  • Q: Documentation on HTTP methods

  • Get revised slides (if done in timely manner)

  • Get revised mock exams to students

Homework

  • For Fri 8 Apr 2022
    • Quizzes for Topic Area #1 (PHP Basics)
    • Quizzes for Topic Area #2 (Data Types and Formats)
    • Quizzes for Topic Area #3 (Strings and Patterns)
  • For Mon 11 Apr 2022
    • First Mock Exam
    • Quizzes for Topic Area #4 (Arrays)
    • Quizzes for Topic Area #5 (I/O)
    • Quizzes for Topic Area #6 (Functions)
  • For Wed 13 Apr 2022
    • Second Mock Exam
    • Quizzes for Topic Area #7 (OOP)
  • For Fri 15 Apr 2022
    • Final Mock Exam
    • Quizzes for all remaining Topics

Docker Container Setup

  • Download the ZIP file from the URL given by the instructor
  • Unzip into a new folder /path/to/zip
  • Follow the setup instructions in /path/to/zip/README.md

TODO

Class Notes

Overview of topics: https://www.zend.com/training/php-certification-exam Good overview of typical PHP program operation:

<?php
namespace abc {
	define('WHATEVER', 'Whatever', TRUE);
	const ANYTHING = 'Anything';
}

namespace xyz {
	echo WHATEVER;
	echo ANYTHING;
}

Bitwise Operators

Tutorial oriented towards the exam:

<?php
echo "Logical AND\n";
printf("%04b\n", 0b00 & 0b00);
printf("%04b\n", 0b00 & 0b01);
printf("%04b\n", 0b01 & 0b00);
printf("%04b\n", 0b01 & 0b01);

echo "Logical OR\n";
printf("%04b\n", 0b00 | 0b00);
printf("%04b\n", 0b00 | 0b01);
printf("%04b\n", 0b01 | 0b00);
printf("%04b\n", 0b01 | 0b01);

echo "Logical XOR\n";
printf("%04b\n", 0b00 ^ 0b00);
printf("%04b\n", 0b00 ^ 0b01);
printf("%04b\n", 0b01 ^ 0b00);
printf("%04b\n", 0b01 ^ 0b01);

Examples of the three ops:

<?php
$a = 0b11111111;
$b = 0b11011101;

printf("%08b", $a & $b); // 1101 1101
printf("%08b", $a | $b); // 1111 1111
printf("%08b", $a ^ $b); // 0010 0010

Left/right shift illustration:

<?php
echo 16 << 3;
echo "\n";
echo 0b10000000;
echo "\n";

echo 16 >> 3;
echo "\n";
echo 0b00000010;
echo "\n";

echo 15 >> 3;
echo "\n";
echo 0b00000001;
echo "\n";

php.ini file settings:

Garbage Collection

Data Formats

Read up on SimpleXMLElement

<?php
$date[] = new DateTime('third thursday of next month');
$date[] = new DateTime('now', new DateTimeZone('CET'));
$date[] = new DateTime('@' . time());
$date[] = (new DateTime())->add(new DateInterval('P3D'));

var_dump($date);
  • Don't forget that to run a SOAP request, you can also use:

    • SoapClient::__soapCall()
    • SoapClient::__doRequest()
  • Study on DateTimeInterval and DateTimeZone and also "relative" time formats

  • In addition, be aware of the basic time format codes

  • https://www.php.net/manual/en/datetime.formats.relative.php

Strings

  • Study the docs on sprintf() to get format codes for that family of functions
  • Example using negative offsets:
<?php
$dir = '/home/doug/some/directory/';
if (substr($dir, 0, 1) === '/') echo 'Leading slash' . PHP_EOL;
if (substr($dir, -1) === '/') echo 'Trailing slash' . PHP_EOL;
if ($dir[-1] === '/') echo 'Trailing slash' . PHP_EOL;
<?php
$text = 'Doug Bierer';
$patt = '/(.*)\s(.*)/';
echo preg_replace($patt, '$2, $1', $text);
  • preg_replace() and preg_match() example using sub-patterns:
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '$2 $1 $3';
echo preg_replace($pattern, $replacement, $string);

preg_match($pattern, $string, $matches);
var_dump($matches);

Arrays

For iterating through an array beginning-to-end don't forget about these functions:

  • array_walk()
  • array_walk_recursive()
  • array_map() Also: please don't forget the array navigation functions:
  • reset(): sets pointer to top
  • end() : sets pointer to end
  • prev() : advances array pointer
  • next() : un-advances array pointer
  • key() : returns index value at array pointer
  • current() : returns value of element at array pointer

I/O

Streams

  • Don't have to study all functions, just certain of the more common ones
  • https://www.php.net/streams
    • stream_context_create()
    • stream_wrapper_register()
    • stream_filter_register()
    • stream_filter_append()
    • stream_socket_client() In addition to the informational file functions mentioned, you also have:
  • fileatime()
  • filemtime()
  • filectime() etc.

Functions

OOP

  • Read up on magic methods!
  • Serialization example:
<?php
class User
{
	public $first = 'Fred';
	public $last  = 'Flintstone';
	public $role  = 'Cavemen';
	public $test  = [1,2,3];
	public $hash  = '';
	public function __construct()
	{
		$this->hash = bin2hex(random_bytes(8));
	}
	public function getName()
	{
		return $this->first . ' ' . $this->last;
	}
	public function __sleep()
	{
		return ['first','last','role','test'];
	}
	public function __wakeup()
	{
		$this->__construct();
	}
}

echo '<pre>';
$user = new User();
echo $user->getName();
echo "\n";

$str = serialize($user);
echo $str . "\n";
var_dump($user);
echo "\n";

$obj = unserialize($str);
echo $obj->getName();
echo "\n";
var_dump($obj);
echo '</pre>';

Database Topic

Fetch Modes:

  • Focus on array and object fetch modes

Security Topic

Questions are drawn from here:

Error Handling

Example of aggregated Catch block:

try {
    $pdo = new PDO($params);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException | Exception $e) {
    error_log('Database error: ' . date('Y-m-d H:i:s'));
} catch (Throwable $e) {
    error_log('Any and all errors or exceptions: ' . date('Y-m-d H:i:s'));
} finally {
    echo 'Database connection ';
    echo ($pdo) ? 'succeeded' : 'failed';
}

Change Request

  • s/ not be "Ksy and Value Introspections"
  • Mock #1:
    • Ques 12: reword 1st answer: "foo element containing the a child node bar tag"
  • Mock #2:
    • Ques: 1: 1st needs to be $str not $data
  • http://localhost:9999/#/9/27
    • Code example doesn't work as written. Correct syntax is:
// build a DSN using "host"
try {
    // create the PDO instance using the DSN + credentials
    $pdo = new PDO( <dsn>, <user>, <password>);
    $result = $pdo->query('SELECT * FROM customers',
                          PDO::FETCH_INTO,
                          new ArrayObject());
    foreach ($result as $obj) echo $obj->firstname . ' ' . $obj->lastname . PHP_EOL;
} catch (PDOException $e) {
    echo get_class($e) . ':' . $e->getMessage();
}
Create a class which implements SessionHandlerInterface and defines these methods: open, read, write, destroy, gc, close
  • Final Mock Exam
    • Question 3: no code!