Skip to content

Latest commit

 

History

History
292 lines (253 loc) · 7.82 KB

php-cert-sep-2021.md

File metadata and controls

292 lines (253 loc) · 7.82 KB

PHP Certification - Sep 2021

TODO

Homework

For Fri 01 Oct:

  • All remaining quiz questions (course modules 9 - 12)
  • Final Mock Exam

For Wed 29 Sep:

  • Quiz questions for course module 8 "OOP"
  • Second Mock Exam

For Mon 27 Sep:

  • Quiz questions for course module 5 "Arrays"
  • Quiz questions for course module 6 "I/O"
  • Quiz questions for course module 7 "Functions"
  • First Mock Exam

For Thu 23 Sep:

  • Quiz questions for course module 3 "Data Formats and Types"
  • Quiz questions for course module 4 "Strings and Patterns"

For Wed 22 Sep:

  • Quiz questions for course module 2 "PHP Basics"

For Fri 24 Sep:

  • Quiz questions for course module 3 "Data Formats ..."

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:

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);

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";
  • Study up on gc_collect_cycles()

Garbage Collection

Have a look at this article: https://www.php.net/manual/en/features.gc.performance-considerations.php

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);

Strings

<?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);

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()

Functions

OOP

Security Topic

Questions are drawn from here:

Change Request

  • 2-57: answers s/be B & C; D answer makes assumption not born out by the question
  • 6-19: s/be:

$options = [
    'http' => [
        'method'  => 'POST',
        'header'  => implode("\r\n", $headers),
        'content' => $query
    ]
];
  • 8-16: "Class Member Overrides"

    • Example doesn't work because the child class fails to define the method in compliance with parent method requirements
  • 8-97:

    • C ans is also correct, can use iterator_to_array() because ArrayObject is iterable
  • This needs a space in front of "16"


$x = 0x10;   $y = 020;  $z = "16";
$x += 0b100; $y += "4"; $z += 04;
echo $x;
echo $y;
echo $z;
  • PDF pg. 202 / 5-17

    • array_search() returns the key (if found), not the value!
  • PDF 8-16: child class definition should match that of the parent class! Otherwise:

Warning: Declaration of Container\ControllerServiceContainer::set(string $name, $value) should be compatible with Container\ServiceContainer::set($name, $value) in /srv/code/test.php on line 20
  • Should be like this:
<?php

namespace Container;
use FactoryInterface;
class ServiceContainer {
    protected $services = [];

    public function set($name, $value) {
        $this->services[$name] = $value;
    }
}

class ControllerServiceContainer extends ServiceContainer {
    protected $factories = [];

    public function set($name, $value) {
        ($value instanceof FactoryInterface) ? $this->factories[$name] = $value :
            parent::set($name, $value);
    }
}
  • 8-22: parent __construct() cannot final in this example! Should be:

namespace Container;
use FactoryInterface;
abstract class ServiceContainer {
    protected const PATH = '/path';
    protected $services = [];
    public function __construct($name, $service) {
        $this->services[$name] = $service;
    }
}
class ControllerServiceContainer extends ServiceContainer {
    protected $factories = [];
    public function __construct(string $name, $value) {
        ($value instanceof FactoryInterface)? $this->factories[$name] = $value :
            parent::__construct($name, $value);
    }
}

$container = new ControllerServiceContainer('created_date', new \Datetime('now'))
try {
    $pdo = new PDO($params);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception | PDOException $e) {
    error_log('Database error: ' . $e->getMessage()));
} catch (Throwable $t) {
    error_log('General error: ' . $t->getMessage()));
} finally {
    echo 'Database connection ';
    echo ($pdo) ? 'succeeded' : 'failed';
}

Mock Exam #2

  • Ques 1
    • First answers use $data so result is impossible
    • D and E are correct: answer key needs updating
    • Change $data to $str and change to choose 3
  • Ques 5
    • Leading sentence bad
  • Ques 12
    • Leading sentence bad

Mock Final Exam:

  • Question 3: missing code block
  • Question 5: maybe not use the word "listing"
  • Question 13: missing \
  • Question 21: need to add "None of the above"
  • Question 25: missing end };
  • Question 27: missing " in 2nd SQL statement