m6 Flashcards
What are Static and Dynamic typing?
What are Strongly and Weakly typed?
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.
What are type casting and type juggling?
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
Name some examples of PHP array functions?
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.
What is a ternary operator and its syntax?
Ternary operator - a shorthand if/else statement
$result = condition ? value1 : value2;
Name some PHP string functions?
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
What are regular expressions? Give an example
What is $matches?
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.
In OOP, what are the Access Modifiers and what each one does?
Public - can be accessed anywhere
Private - can be accessed only within the object
Protected - can be access within the object and its descendants
What is the difference between static and instance variables in a Class?
Describe the uses, benefits and limitations of static methods?
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.
What is an entity class?
An entity class is a class that represents a real-world entity.
It is often a row from a database table.
What is the Hydrator pattern?
What is the syntax?
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);
What are Magic methods with some examples?
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
What is an interface and how is it different to an abstract class?
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
What is Overriding?
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.
What do the parent and final keywords mean?
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
What does SOLID stand for and what does it mean?
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
What is the Single Responsibility Principle?
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.
What is the Open-Closed principle?
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.
What is the Liskov Substitution principle?
When extending a class, your child class should be substitutable for your parent class in every way.
What is the Interface Segregation principle?
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.
What is the Dependency Inversion principle?
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
What are Namespaces?
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.
What are Exeptions and what is the syntax?
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)
What is the difference between a design pattern and an anti pattern?
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.
What would the pattern be for connection to a database?
connect to db using new PDO, then getData we do the SELECT sql statement then need to prepare then execute then fetch