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