The document discusses several key object-oriented programming concepts including:
- Different types of SQL joins like inner, left, right, full outer, and left outer joins.
- The static keyword in PHP and how it allows properties and methods to be accessed without object instantiation.
- Visibility levels like public, private, and protected for class properties and methods.
- Abstract classes and interfaces, how they are similar but interfaces define contracts while abstract classes can contain partial implementations.
- The difference between exceptions which are thrown intentionally and errors which occur unintentionally.
- The difference between passing arguments by value which passes a copy versus by reference which passes a reference to the original variable.
1 of 6
Download to read offline
More Related Content
SQL Joins
1. Some of these notes are specific to PHP however the concepts typically apply to any OOP
language. For most programmers these concepts are probably already embedded in their
brains. For those of us who werent computer science majors or have been stuck on a project
or framework for an extended period of time this will be a good refresher as most employers
will ask questions related to these concepts in the early stages of an interview or technical
evaluation.
Different SQL JOINs
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
INNER JOIN: produces only the set of records that match in both Table A and Table B.
SELECT * FROM TableA
INNER JOIN TableB
ON TableA.name = TableB.name
FULL OUTER JOIN: produces the set of all records in Table A and Table B, with matching
records from both sides where available. If there is no match, the missing side will contain null.
SELECT * FROM TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name
LEFT OUTER JOIN: produces a complete set of records from Table A, with the matching
records (where available) in Table B. If there is no match, the right side will contain null.
SELECT * FROM TableA
LEFT OUTER JOIN TableB
ON TableA.name = TableB.name
To produce the set of records only in Table A, but not in Table B, we perform the same left outer
join, then exclude the records we don't want from the right side via a where clause.
SELECT * FROM TableA
LEFT OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableB.id IS null
To produce the set of records unique to Table A and Table B, we perform the same full outer
join, then exclude the records we don't want from both sides via a where clause.
2. SELECT * FROM TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableA.id IS null
OR TableB.id IS null
Static Keyword
Declaring class properties or methods as static makes them accessible without needing an
instantiation of the class. A property declared as static can not be accessed with an instantiated
class object (though a static method can).
For compatibility with PHP 4, if no visibility declaration is used, then the property or method will
be treated as if it was declared as public.
Because static methods are callable without an instance of the object created, the pseudo-
variable $this is not available inside the method declared as static.
Static properties cannot be accessed through the object using the arrow operator ->.
Calling non-static methods statically generates an E_STRICT level warning.
Like any other PHP static variable, static properties may only be initialized using a literal or
constant; expressions are not allowed. So while you may initialize a static property to an integer
or array (for instance), you may not initialize it to another variable, to a function return value, or
to an object.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can
not be a keyword (e.g. self, parent and static).
Visibility
The visibility of a property or method can be defined by prefixing the declaration with the
keywords public, protected or private. Class members declared public can be accessed
everywhere.
Protected - can be accessed only within the class itself and by inherited and parent classes.
Private - may only be accessed by the class that defines the member.
Public - can be accessed from anywhere
Property Visibility
Class properties must be defined as public, private, or protected. If declared using var without
an explicit visibility keyword, the property will be defined as public.
3. Method Visibility
Class methods may be defined as public, private, or protected. Methods declared without any
explicit visibility keyword are defined as public.
Visibility from other objects
Objects of the same type will have access to each others private and protected members even
though they are not the same instances. This is because the implementation specific details are
already known when inside those objects.
Abstract Classes & Interfaces
Abstract Classes
An abstract class is a class that is only partially implemented by the programmer. It may contain
one or more abstract methods. An abstract method is simply a function definition that serves to
tell the programmer that the method must be implemented in a child class.
Interfaces
An interface is similar to an abstract class; indeed interfaces occupy the same namespace as
classes and abstract classes. For that reason, you cannot define an interface with the same
name as a class. An interface is a fully abstract class; none of its methods are implemented and
instead of a class sub-classing from it, it is said to implement that interface. An interface defines
a contract that implementing classes must fulfill. An interface definition consists of signatures of
public members, without any implementing code.
Lets say you had three classes - BedroomDoor, Gate and GarageDoor. Lets also imagine the
BedroomDoor and GarageDoor (and all other Doors) have a range of varying behaviour, but
both have exactly the same locking mechanisms. So we have:
PHP Code:
class Gate {
// gate specific behaviour
}
class BedroomDoor {
// bedroom door specific behaviour
public function lock($key) {
$this->lock->insert_key($key);
$key->turn('clockwise');
}
4. }
class GarageDoor {
// garage door specific behaviour
public function lock($key) {
$this->lock->insert_key($key);
$key->turn('clockwise');
}
}
Now, as you can see the two lock methods are exactly the same - duplication is evil!
All Doors can be locked in the same way (but not Gates, these are bolted shut). We can
eliminate this duplication by introducing an abstract class Door which implements this
lock method for all of the concrete instances, eliminating the duplication.
PHP Code:
abstract class Door {
public function lock($key) {
$this->lock->insert_key($key);
$key->turn('clockwise');
}
}
class BedroomDoor extends Door {
// bedroom door specific behaviour
}
class GarageDoor extends Door {
// garage door specific behaviour
}
Note that we don't necessarily have to use an abstract class to eliminate the
duplication, we could just inherit from a normal concrete Door class that can be
instantiated in its own right, depending on whether it makes sense for a Door to be
instantiated on its own.
Now, interfaces - interfaces can be used to specify a specific set of behaviour and that
behaviour's API which classes can implement. As explained above, interfaces are best
looked at as adjectives. Both doors and gates can be opened. Thats an interface right
there:
PHP Code:
interface Openable {
public function open() { }
5. }
Both doors (through the abstract Door class) and the gate can be opened, so they can
implement the interface.
PHP Code:
class Gate implements Openable {
public function open() {
if($this->is_locked()) {
$this->unlock();
}
$this->leftSide()->pullOpen();
$this->rightSide()->pullOpen();
}
}
abstract class Door implements Openable {
// our abstract class defines the open function
// as abstract as we open different types of door
// differently
abstract public function open() {}
}
class GarageDoor {
public function open() { // implement here }
}
class BedroomDoor {
public function open() { // implement here }
}
By specifying that we will implement the Openable interface, the class is bound by that
contract and you will get an error if you forget to implement any of the methods in the
interface. Finally, you can take advantage of type hinting in your methods - lets say you
need to pass an object into another class to do something - the receiving class doesn't
care what object we pass it as long as it can be opened. We can enforce this using type
hinting:
PHP Code:
class Foo {
public function openSomething(Openable $arg) {
$arg->open();
}
}
Doing the above ensures that whatever gets passed to the openSomething() method
will respond to open() because it implements the Openable interface.
6. Error vs Exception
Exceptions are thrown intentionally by code using a throw, errors... not so much.
Errors come about as a result of something which isn't handled typically. (IO errors, TCP/IP
errors, null reference errors)
Pass by value vs Pass by reference
Pass by value - A copy of the value of your variable is made, and that copy is passed to the
function you have called.
A function or subroutine that is passed a variable by value is working with a copy of that
variables contents. The original variables contents are not affected by what happens inside of
your subroutine. The scope of the copy is restricted to the lifetime of your subroutine. Upon exit,
the copy will likely be taken care of by garbage collection unless you explicitly take some sort of
action to keep it around, such as returning its value to caller.
Pass by references - A reference to location of the original variables contents is passed to the
function.
A function that takes a value by reference is given the address of the location in memory where
the variables value can be found. Any statements in the function then operate on the original
variables contents. Changes made will persist after your function or subroutine has run its
course.