m6 Flashcards

1
Q

What are Static and Dynamic typing?

What are Strongly and Weakly typed?

A

Static typing: Have to explicitly state the type when declaring a variable. E.g. Java, C#, C++ and Go.

Dynamic typing: No explicit type stated.

Strongly typed: The compiler will not automatically convert types to suit an operation.

Weakly typed: The compiler will automatically convert types to suit an operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are type casting and type juggling?

A

Type casting - force a variable to be evaluated in a certain way

Type juggling - conversion of values to a common data type when comparing values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name some examples of PHP array functions?

A

is_array(var) - Finds whether or not the given variable is an array.

count(var) - Counts all the elements in a given array (or object).

in_array($needle, $haystack) - Searches for the needle in the haystack. Returns true/false if found/not found.

array_key_exists($key, $array) - Searches the given array for the existence of the given key. Returns true/false.

array_merge($array…) - Merges one or more arrays together, Returns the resulting array.

array_map($callback, $array…) - Applies a callback to the elements of the provided arrays. Returns the resulting array (but leaves the original unchanged).

array_filter($array, $callback) - Passes each element to the callback. If the callback returns TRUE, the element is included in the result array.

array_reduce($array, $callback [$initial]) Reduces the array to a single value using the callback function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a ternary operator and its syntax?

A

Ternary operator - a shorthand if/else statement

$result = condition ? value1 : value2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name some PHP string functions?

A

substr() - returns part of a string

str_shuffle() - Randomly shuffles a string

trim() - trims whitespace (or other characters) from the beginning and end of a string

ltrim() - trim for start only

rtrim() - trim for end only

explode() - split a string and puts into an array

implode() - joins elements of an array into a string

htmlentities() - Convert all applicable characters to HTML entities

strpos() - Find the position of the first occurrence of a substring in a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are regular expressions? Give an example

What is $matches?

A

Regular expressions - Searches subject for a match to the regular expression given in a pattern.

preg_match_all(‘/bacon/’,$text,$matches);

preg_match() returns 1(or the number of matches for preg_match_all) if the pattern matches given subject, 0 if it does not, or false on failure.

If matches is provided, then it is filled with the results of search.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In OOP, what are the Access Modifiers and what each one does?

A

Public - can be accessed anywhere
Private - can be accessed only within the object
Protected - can be access within the object and its descendants

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between static and instance variables in a Class?
Describe the uses, benefits and limitations of static methods?

A

Static variables belong to the class itself, not to objects. There is only one copy of a static variable, and it is shared among all the instances.

declared public static int $numInPack = 0

Accessed within the class using self::$...
\:: is the scope resolution operator.

Accessed outside the class using Classname::$…

Benefit of static method:
An object does not have to be instantiated.
Limitation of static method:
Cannot access the $this context

Use of static:
Common properties and methods.
Libraries. E.g. for helper functions
Singleton design pattern.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an entity class?

A

An entity class is a class that represents a real-world entity.

It is often a row from a database table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the Hydrator pattern?

What is the syntax?

A

Hydration refers to the process of filling an object with data.

public static function populateFromArray(array $pigArray, Pig1 $pig)
{
$pig->name = $pigArray[0];
}

PigHydrator::populateFromArray($pigArray, $sally);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are Magic methods with some examples?

A

Magic methods are special methods that override PHP’s default’s action when certain actions are performed on an object.

PHP will call these functions “behind the scenes”.
Examples:
__construct() - called for the initialisation of a new object
__destruct() - called as soon as there are no other references to a particular object,
__toString() - allows us to treat the object as a string
__invoke() - allows us to treat the object as a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an interface and how is it different to an abstract class?

A

An abstract class is a category of classes, whereas an interface determines what a class should be able to do.

Abstract classes can contain functionality, but interfaces can’t.

Interfaces show what functions should be there but not what they actually do.

A class can only extend one abstract, but can implement multiple interfaces.

An abstract class can be a bit restrictive, interfaces allow for more mix and matchning for different classes

Interfaces help structure code and make it more readable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Overriding?

A

To override a method, you redefine that method in the child class with the same name, parameters, and return type.

It is done when you want it to have different functionality to its parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What do the parent and final keywords mean?

A
The parent keyword allows a child method to call a parent method.
The final keyword stops a child class overriding a parent method.
Use final when you know you dont want any further inheritance from that class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does SOLID stand for and what does it mean?

A

SOLID is an acronym for five principles that help software developers design maintainable and extendable classes.

S - single responsibility
O - Open-closed principle
L - Liskov substitution principle
I - Interface segregation principle
D - Dependency inversion principle
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the Single Responsibility Principle?

A

Any class should only have one “responsibility”.

The only things that can go into the class are things that relate to the single responsibility.

It makes your software easier to implement and prevents unexpected side-effects of future changes.

17
Q

What is the Open-Closed principle?

A

A class should be open for extension, and closed for modification.

Write code so you can add new functionality without changing the existing code.
changing an old class could cause issues in the application, so best to implement a new interface to override the class functionality.

Generally, interfaces are the preferred technique. Interfaces and their implementations should be independent. So, adding and implementing a new interface will not damage existing code.

18
Q

What is the Liskov Substitution principle?

A

When extending a class, your child class should be substitutable for your parent class in every way.

19
Q

What is the Interface Segregation principle?

A

A class should not implement an interface where it does not require all of the methods from that interface.

Make interfaces as small as possible, even one method each, and implement as many interfaces as you need.

20
Q

What is the Dependency Inversion principle?

A

Entities must depend on abstractions not on concretions. (Concretion means class.)

Don’t type hint to the name of a class, this makes the code very un-scale-able.

Type hint using interfaces or abstract classes.

I.e using the function (Shape $shape ) example, means we can add new shapes makingthe code scalable

21
Q

What are Namespaces?

A

You cannot create two classes with the same name. Or it may clash with a library you are using. So we can use Namespaces to create scope for each class.

Only four types of code are affected by namespaces: classes, interfaces, functions and constants.

22
Q

What are Exeptions and what is the syntax?

A

Exceptions are used to change the normal flow of a script if a specified error occurs.

Exceptions can be thrown and caught to deal with potential errors.

throw new InvalidArgumentException('Division by zero.')
Try {something} catch (Exception $e)
23
Q

What is the difference between a design pattern and an anti pattern?

A

DP - A solution to a common problem, which is generally considered a good way of solving the problem.

AP - A bad solution to a common problem.

24
Q

What would the pattern be for connection to a database?

A
connect to db using new PDO, 
then getData we do the SELECT sql statement 
then need to prepare 
then execute
then fetch
25
Q

What is the Value Object pattern?

A

Design pattern

A small simple object, like money or a date range, whose equality isn’t based on identity

VO gets around primitive obsession by encapsulating behaviour to an object that would otherwise just be a primitive

Value Objects should be immutable - should not change.

26
Q

What is the Adapater pattern?

A

A way of converting the ‘interface’ of a class to match what another class expects.

Adapters are helpful if you want to use a class that doesn’t have quite the exact methods you need, and you can’t change the orignal class.

The adapter can take the methods you can access in the original class, and adapt them into the methods you need.

27
Q

What is the Singleton pattern?

A

Either a design pattern or an anti-pattern, depending on who you talk to.

Ensure a class has only one instance, and provide a global point of access to it.

An example of where you might want this is for storing application settings.

Check list
Define a private static attribute in the "single instance" class.
Define a public static accessor function in the class.
Do "lazy initialization" (creation on first use) in the accessor function.
I.e use self::$instance = new Singleton();
Define all constructors to be protected or private.
Clients may only use the accessor function to manipulate the Singleton.
28
Q

What is the God Class pattern?

A

An anti-pattern.

A single class that contains loads of stuff - it just does everything the app needs to do.

Somebody creates a class that’s designed to do one thing then somebody adds to the class for a new feature and now it does more than one thing, constantly getting built up until loads depends on the God Class and it becomes impossible to maintain.

29
Q

What is the Golden Hammer pattern?

A

An anti-pattern.

You learn a new tool, and you use it to solve your problem.

You then get a different problem, and automatically go to the new tool you learned.

You then just fall back into using the same tool for every problem you face.

30
Q

What is Autoloading and its benefits?

A

Autoloading is designed to prevent having to use loads of require statements.

Autoloading will automatically load in files by class name, interface name, and abstract class name.

The latest standard for autoloading is PSR-4

It uses a config file called composer.json

31
Q

What does the PSR-4 standard require for Autoloading?

A

Classes, interfaces, abstract classes and namespaces must start with a capital letter.

The file that your class is in must match the name of your class exactly.

Your namespaces must match your directory structure.

All files/namespaces have to be within one root namespace.

Your root namespace must be configured in your composer settings.

32
Q

What is composer?

A

A package manager

A package manager is a software tool used to automate the process of installing, upgrading, configuring, and removing software.

i.e composer dump-autoload
generates (or regenerates) the autoloading code that will create a list of all the
classes that need to be included in the project.

33
Q

What are the types of unit tests?

What are the 3 A’s?

A

Tests have 3As - Arrange Act Assert

3 types of test:
Success test - Does the function work when it is used properly?
Failure test - Does the function do what it is supposed to do if it is not used properly? I.e input outside of range
Malformed test - How does the function handle incorrect data types

34
Q

What is Mocking in unit testing?

A

A mock is a piece of dummy code that helps your tests run in a way that isolates specific functionality.

Example, creating a dummy response from another class to simulate a real response.

35
Q

What is Stubbing in unit testing? Provide syntax and what cannot be stubbed.

A

replacing an object with a test double that returns configured return values is referred to as stubbing

$food->expects($this->once())

  • > method(‘getFoodType’)
  • > willReturn(‘grass’);

final, private, protected, and static methods cannot be stubbed or mocked as they are ignored by PHPUnit’s test

36
Q

What is cURL and what is the pattern for it?

A

cURL allows you to do a fetch in server side code.

Design Pattern
initialize a cURL session using the curl_init(),
then you can set all your options for the transfer via the curl_setopt(),
then you can execute the session with the curl_exec()
then you finish off your session using the curl_close().

37
Q

What is a Session?

A

Session - Sessions store data about user interaction in a file on the server

A session is created by the first HTTP request to the server and exists until the last HTTP response.

Normal cookies are not trusted because an end user can change it. Sessions are trusted because we are in control of them.

Session data is stored in a superglobal called $_SESSION.

38
Q

What are filter validation and sanitisation? Some examples of each

A

Sanitising and validating are ways of protecting our code and database from malicious data (HTML injection, SQL injection, XSS).

Validating returns false if the input doesn’t match given criteria (syntax/format).
Sanitising removes illegal characters.

Input validation is stricter than sanitising. Rather than merely “cleaning” the incoming data, we’re ensuring it adheres to a very specifically-defined format or rejecting it entirely

Validation examples
FILTER_VALIDATE_EMAIL
FILTER_VALIDATE_FLOAT
FILTER_VALIDATE_URL

Sanitisation examples
FILTER_SANITIZE_EMAIL
FILTER_SANITIZE_STRING
FILTER_SANITIZE_NUMBER_FLOAT