Object Oriented Concepts Flashcards Preview

PHP-OOP > Object Oriented Concepts > Flashcards

Flashcards in Object Oriented Concepts Deck (48):
1

Class −

This is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.

2

Class −

This is a programmer-defined data type, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.

3

Object −

An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.

4

Member Variable −

These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.

5

Member function

− These are the function defined inside a class and are used to access object data.

6

Inheritance −

When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.

7

Parent class −

A class that is inherited from by another class.
This is also called a base class or super class.

8

Child Class

A class that inherits from another class.
This is also called a subclass or derived class.

9

What is Polymorphism

This is an object oriented concept where same function can be used for different purposes.
For example function name will remain same but it make take different number of arguments and can do different task.

10

Overloading −

a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.

11

Data Abstraction

Any representation of data in which the implementation details are hidden (abstracted).

12

Encapsulation

Refers to a concept where we encapsulate all the data and member functions together to form an object.

13

Constructor

− refers to a special type of function which will be called automatically whenever there is an object formation from a class.

14

Destructor −

Refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.

15

Class definition

16

Object −

An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.

17

Member Variable −

These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.

18

Member function

− These are the function defined inside a class and are used to access object data.

19

Inheritance −

When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.

20

Parent class −

A class that is inherited from by another class.
This is also called a base class or super class.

21

Child Class

A class that inherits from another class.
This is also called a subclass or derived class.

22

What is Polymorphism

This is an object oriented concept where same function can be used for different purposes.
For example function name will remain same but it make take different number of arguments and can do different task.

23

Overloading −

a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.

24

Data Abstraction

Any representation of data in which the implementation details are hidden (abstracted).

25

Encapsulation

Refers to a concept where we encapsulate all the data and member functions together to form an object.

26

Constructor

− refers to a special type of function which will be called automatically whenever there is an object formation from a class.

27

Destructor −

Refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.

28

Class definition

29

Here is an example which defines a class of Books type −

price = $par;
}

function getPrice(){
echo $this->price ."
";
}

function setTitle($par){
$this->title = $par;
}

function getTitle(){
echo $this->title ."
";
}
}
?>

30

Creating Objects in PHP

Following is an example of how to create object using new operator.

$physics = new Books;
$maths = new Books;
$chemistry = new Books;

31

Calling Member Functions

$physics->setTitle( "Physics for High School" );
$chemistry->setTitle( "Advanced Chemistry" );
$maths->setTitle( "Algebra" );

$physics->setPrice( 10 );
$chemistry->setPrice( 15 );
$maths->setPrice( 7 );

32

What is a Constructor Functions

Constructor Functions are special type of functions which are called automatically whenever an object is created. So we take full advantage of this behaviour, by initializing many things through constructor functions.

33

How do you define a Constructor Function

function __construct( $par1, $par2 ) {
$this->title = $par1;
$this->price = $par2;
}

34

Use constructor function

$physics = new Books( "Physics for High School", 10 );
$maths = new Books ( "Advanced Chemistry", 15 );
$chemistry = new Books ("Algebra", 7 );

/* Get those set values */
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();

$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();

35

Destructor

Like a constructor function you can define a destructor function using function __destruct(). You can release all the resources with-in a destructor.

36

Public Members

Unless you specify otherwise, properties and methods of a class are public. That is to say, they may be accessed in three possible situations −

From outside the class in which it is declared

From within the class in which it is declared

From within another class that implements the class in which it is declared

37

Private members

By designating a member private, you limit its accessibility to the class in which it is declared. The private member cannot be referred to from classes that inherit the class in which it is declared and cannot be accessed from outside the class.

A class member can be made private by using private keyword infront of the member.

38

A class member can be made private by using private keyword infront of the member.

class MyClass {
private $car = "skoda";
$driver = "SRK";

function __construct($par) {
// Statements here run every time
// an instance of the class
// is created.
}

function myPublicFunction() {
return("I'm visible!");
}

private function myPrivateFunction() {
return("I'm not visible outside!");
}
}

39

Protected members

A protected property or method is accessible in the class in which it is declared, as well as in classes that extend that class. Protected members are not available outside of those two kinds of classes. A class member can be made protected by using protected keyword in front of the member.

40

Here is different version of MyClass

class MyClass {
protected $car = "skoda";
$driver = "SRK";

function __construct($par) {
// Statements here run every time
// an instance of the class
// is created.
}

function myPublicFunction() {
return("I'm visible!");
}

protected function myPrivateFunction() {
return("I'm visible in child class!");
}
}

41

Interfaces

Interfaces are defined to provide a common function names to the implementers.
Different implementors can implement those interfaces according to their requirements.
You can say, interfaces are skeletons which are implemented by developers.

42

Interfaces

interface Mail {
public function sendMail();
}

Then, if another class implemented that interface, like this

class Report implements Mail {
// sendMail() Definition goes here
}

43

Constants

A constant is somewhat like a variable, in that it holds a value, but is really more like a function because a constant is immutable. Once you declare a constant, it does not change.

44

Declaring one constant is easy, as is done in this version of MyClass

class MyClass {
const requiredMargin = 1.7;

function __construct($incomingValue) {
// Statements here run every time
// an instance of the class
// is created.
}
}

45

Abstract Classes

An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this

46

When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same visibility.

abstract class MyAbstractClass {
abstract function myAbstractFunction() {
}
}

47

Static Keyword

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).

48

Static Keyword try out following example

staticValue() . "\n";
?>