SlideShare une entreprise Scribd logo
1  sur  6
Télécharger pour lire hors ligne
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 weren’t 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.
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.
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.

Let’s 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');
       }
}

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() { }
}
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.
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.

Contenu connexe

Tendances

Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classesAKANSH SINGHAL
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsqlArun Sial
 
PLSQL CURSOR
PLSQL CURSORPLSQL CURSOR
PLSQL CURSORArun Sial
 
Notes of Important Programming Fundamental Questions
Notes of Important Programming Fundamental QuestionsNotes of Important Programming Fundamental Questions
Notes of Important Programming Fundamental QuestionsAdeel Rasheed
 
Templates and Exception Handling in C++
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++Nimrita Koul
 
Control structures i
Control structures i Control structures i
Control structures i Ahmad Idrees
 
Hiding the implementation in c++
Hiding the implementation in c++Hiding the implementation in c++
Hiding the implementation in c++Daniel Rodríguez
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 

Tendances (19)

Opps concept
Opps conceptOpps concept
Opps concept
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
 
PLSQL CURSOR
PLSQL CURSORPLSQL CURSOR
PLSQL CURSOR
 
Notes of Important Programming Fundamental Questions
Notes of Important Programming Fundamental QuestionsNotes of Important Programming Fundamental Questions
Notes of Important Programming Fundamental Questions
 
Templates and Exception Handling in C++
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++
 
Control structures i
Control structures i Control structures i
Control structures i
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
code analysis for c++
code analysis for c++code analysis for c++
code analysis for c++
 
Acquiring a solid foundation in java
Acquiring a solid foundation in javaAcquiring a solid foundation in java
Acquiring a solid foundation in java
 
Hiding the implementation in c++
Hiding the implementation in c++Hiding the implementation in c++
Hiding the implementation in c++
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
C# XML documentation
C# XML documentationC# XML documentation
C# XML documentation
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
C language 2
C language 2C language 2
C language 2
 

En vedette (13)

SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
May 12, 2013 10:10 PM
May 12, 2013 10:10 PMMay 12, 2013 10:10 PM
May 12, 2013 10:10 PM
 
DEV-27140 SCHEDULED
DEV-27140 SCHEDULEDDEV-27140 SCHEDULED
DEV-27140 SCHEDULED
 
Test the error 2
Test the error 2Test the error 2
Test the error 2
 
Ruben silva hardware.ppt
Ruben silva hardware.pptRuben silva hardware.ppt
Ruben silva hardware.ppt
 
It esencials 4 angelik
It esencials 4 angelikIt esencials 4 angelik
It esencials 4 angelik
 
May 12, 2013 9:45 PM
May 12, 2013 9:45 PMMay 12, 2013 9:45 PM
May 12, 2013 9:45 PM
 
Test the error 3
Test the error 3Test the error 3
Test the error 3
 
Vorrei regalareunaserenata
Vorrei regalareunaserenataVorrei regalareunaserenata
Vorrei regalareunaserenata
 
Inteligent test
Inteligent testInteligent test
Inteligent test
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
Capabilities
CapabilitiesCapabilities
Capabilities
 
Inteligent test
Inteligent testInteligent test
Inteligent test
 

Similaire à OOP Concepts for Programmers

Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.pptVarunP31
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3Naga Muruga
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfBapanKar2
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulationİbrahim Kürce
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdfvenud11
 
Application package
Application packageApplication package
Application packageJAYAARC
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 

Similaire à OOP Concepts for Programmers (20)

Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
C# interview
C# interviewC# interview
C# interview
 
Opps
OppsOpps
Opps
 
Interfaces .ppt
Interfaces .pptInterfaces .ppt
Interfaces .ppt
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.ppt
 
Java 06
Java 06Java 06
Java 06
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods EncapsulationOCA Java SE 8 Exam Chapter 4 Methods Encapsulation
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
 
PHP 5
PHP 5PHP 5
PHP 5
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 
Application package
Application packageApplication package
Application package
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
 
Java interface
Java interfaceJava interface
Java interface
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 

OOP Concepts for Programmers

  • 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 weren’t 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. Let’s 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.