0% found this document useful (0 votes)
52 views36 pages

Oop Reviewer

The document provides an overview of key PHP concepts including: - PHP is moving towards object-oriented programming as frameworks like Laravel are object-oriented and parts of WordPress are becoming object-oriented. - PHP is case sensitive for variables but not for keywords, classes, functions or user-defined functions. - PHP has different variable scopes: local, global, and static. The global and static keywords allow access to global and static variables from within functions. - echo and print are used to output in PHP. echo can take multiple parameters while print only takes one, and echo is marginally faster.

Uploaded by

jakesim423
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views36 pages

Oop Reviewer

The document provides an overview of key PHP concepts including: - PHP is moving towards object-oriented programming as frameworks like Laravel are object-oriented and parts of WordPress are becoming object-oriented. - PHP is case sensitive for variables but not for keywords, classes, functions or user-defined functions. - PHP has different variable scopes: local, global, and static. The global and static keywords allow access to global and static variables from within functions. - echo and print are used to output in PHP. echo can take multiple parameters while print only takes one, and echo is marginally faster.

Uploaded by

jakesim423
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

REVIEWER:

MODULE 01: INTRODUCTION TO PHP


Lesson 01: PHP Syntax, Outputs, Comments and
Variables
Object oriented programming came late to PHP. It
has been around in other languages, like C++, Ruby,
Python, and JavaScript for much longer, but its
demand in PHP is becoming stronger.

After all, most PHP code is used in WordPress, and


WordPress is not written with object-oriented code
at its core. In addition, there are great examples of PHP Case Sensitivity
applications that are written in a procedural style of In PHP, keywords (e.g. if, else, while, echo, etc.),
PHP. But it does not tell the full story. Consider this: classes, functions, and user-defined functions are
not case-sensitive. In the example below, all three
• Procedural programming is inefficient. echo statements below are equal and legal:
• PHP frameworks, like Laravel, Symfony, and
CodeIgniter rely exclusively on object
oriented
• PHP.
• CMS systems, like Drupal 8 and October are object
oriented at their core.
• Every day, additional parts of WordPress are
becoming object oriented.
PHP as it’s known today is actually the successor to
a product named PHP/FI. Created in
1994 by Rasmus Lerdorf, the very first incarnation of
PHP was a simple set of Common
Gateway Interface (CGI) binaries written in the C However; all variable names are case-sensitive!
programming language. Look at the example below; only the first statement
will display the value of the variable! This is because
Basic Syntax $color, $COLOR, and $coLOR are treated as three
A PHP script can be placed anywhere in the different variables:
document. A PHP script starts with <?php and
ends with ?>. The default file extension for PHP files
is “.php”.

PHP Comments
A comment in PHP code is a line that is not executed
as a part of the program. Its only purpose is to be
read by someone who is looking at the code.
Comments can be used to:
A PHP file normally contains HTML tags, and some • Let others understand your code
PHP scripting code. • Remind yourself of what you did - Most
Below, we have an example of a simple PHP file, programmers have experienced coming back to
with a PHP script that uses a built-in PHP function their own work a year or two later and having to re-
“echo” to output the text “Hello World!” on a web figure out what they did. Comments can remind you
page: of what you were thinking when you wrote the code
Syntax for single-line comments:

PHP is a Loosely Typed Language. In the example


above, notice that we did not have to tell PHP which
Syntax for multiple-line comments:
data type the variable is. PHP automatically
associates a data type to the variable, depending on
its value. Since the data types are not set in a strict
sense, you can do things like adding a string to an
integer without causing an error. In PHP 7, type
declarations were added. This gives an option to
specify the data type expected when declaring a
function, and by enabling the strict requirement, it
Using comments to leave out parts of the code: will throw a “Fatal Error” on a type mismatch.

PHP Variables Scope


In PHP, variables can be declared anywhere in the
script. The scope of a variable is the part of the script
PHP Variables where the variable can be referenced/used.
A variable can have a short name (like x and y) or a PHP has three different variable scopes:
more descriptive name (age, carname, 1. local
total_volume). 2. global
Rules for PHP variables: 3. static
• A variable starts with the $ sign, followed by the Global and Local Scope. A variable declared outside
name of the variable a function has a GLOBAL SCOPE and can only be
• A variable name must start with a letter or the accessed outside a function:
underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z,
0-9, and _ )
• Variable names are case-sensitive ($age and
$AGE are two different variables.

Output Variables
The PHP echo statement is often used to output
data to the screen. The following example will show A variable declared within a function has a LOCAL
how to output text and a variable: SCOPE and can only be accessed within that
function:

The following example will produce the same output


as the example above:
PHP The global Keyword. The global keyword is used
to access a global variable from within a function. To
do this, use the global keyword before the variables
(inside the function):

Then, each time the function is called, that variable


will still have the information it contained from the
last time the function was called.
PHP also stores all global variables in an array
called $GLOBALS [index]. The index holds the name PHP echo and print Statements
of the variable. This array is also accessible from With PHP, there are two basic ways to get output:
within functions and can be used to update global echo and print. In this tutorial we use echo or print
variables directly. The example above can be in almost every example. So, this chapter contains a
rewritten like this: little more info about those two output statements.
echo and print are more or less the same. They are
both used to output data to the screen. The
differences are small: echo has no return value
while print has a return value of 1 so it can be used
in expressions. echo can take multiple parameters
(Although such usage is rare) while print can take
one argument. echo is marginally faster than print.

PHP echo Statement. The echo statement can be


used with or without parentheses: echo or echo().
PHP The static Keyword. Normally, when a function
is completed/executed, all of its variables are
deleted. However, sometimes we want a local
variable NOT to be deleted.
The following example shows how to output text
We need it for a further job. To do this, use the static
with the echo command (notice that the text can
keyword when you first declare the variable:
contain HTML markup).

The following example shows how to output text and


variables with the echo statement:
The PHP print Statement. The print statement can
be used with or without parentheses: print or
PHP Integer. An integer data type is a non-decimal
print().The following example shows how to output
number between -2,147,483,648 and
text with the print command (notice that the text can
2,147,483,647.
contain HTML markup):
Rules for integers:
• An integer must have at least one digit
• An integer must not have a decimal point
• An integer can be either positive or negative
• Integers can be specified in: decimal (base 10),
hexadecimal (base 16), octal (base 8), or binary
(base 2) notation
The following example shows how to output text and
variables with the print statement: In the following example $x is an integer. The PHP
var_dump() function returns the data type and
value:

PHP Float. A float (floating point number) is a


number with a decimal point or a number in
exponential form. In the following example $x is a
float. The PHP var_dump() function returns the data
type and value:

PHP Data Types


Variables can store data of different types, and
different data types can do different things.
PHP supports the following data types:
PHP Boolean. A Boolean represents two possible
• String
states: TRUE or FALSE. Booleans are often used in
• Integer
conditional testing.
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP String. A string is a sequence of characters, like
“Hello world!”. A string can be any text inside
quotes. You can use single or double quotes:
LESSON 02: PHP OPERATORS

What is Operator? Simple answer can be given Assignment Operators


using expression 4 + 5 is equal to 9. Here 4 and 5 are There are following logical operators supported by
called operands and + is called operator. PHP language.

PHP language supports following type of


operators.
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators

Arithmetic Operators
There are following arithmetic operators supported Conditional Operator
by PHP language. There is one more operator called conditional
Assume variable A holds 10 and variable B holds 20. operator. This first evaluates an expression for a true
or false value and then execute one of the two given
statements depending upon the result of the
evaluation. The conditional operator has this
syntax:

Comparison Operators
Operators Categories
There are following comparison operators
All the operators we have discussed above can be
supported by PHP language.
categorized into following categories:
Assume variable A holds 10 and variable B holds 20.
• Unary prefix operators, which precede a single
operand.
• Binary operators, which take two operands and
perform a variety of arithmetic and
logical operations.
• The conditional operator (a ternary operator),
which takes three operands and
evaluates either the second or third expression,
depending on the evaluation of the
first expression.
• Assignment operators, which assign a value to a
variable.
Precedence of PHP Operators
Logical Operators
Operator precedence determines the grouping of
There are following logical operators supported by
terms in an expression. This affects how
PHP language.
an expression is evaluated. Certain operators have
Assume variable A holds 10 and variable B holds 20.
higher precedence than others; for
example, the multiplication operator has higher
precedence than the addition operator �
For example x = 7 + 3 * 2; Here x is assigned 13, not
20 because operator * has higher
precedence than + so it first get multiplied with 3*2
and then adds into 7.
Here operators with the highest precedence appear • GET can’t be used to send binary data, like images
at the top of the table, those with the or word documents, to the server.
lowest appear at the bottom. Within an expression, • The data sent by GET method can be accessed
higher precedence operators will be using QUERY_STRING environment
evaluated first. variable.
• The PHP provides $_GET associative array to
access all the sent information using
GET method.
Try out following example by putting the source
code in a PHP script.

LESSON 03:PHP INPUTS USING GET AND POST


METHOD
There are two ways the browser client can send
information to the web server.

• The GET Method


• The POST Method
Before the browser sends the information, it
encodes it using a scheme called URL
encoding. In this scheme, name/value pairs are
joined with equal signs and different pairs The POST Method
are separated by the ampersand. The POST method transfers information via HTTP
headers. The information is encoded as
name1=value1&name2=value2&name3=value3 described in case of GET method and put into a
header called QUERY_STRING.
Spaces are removed and replaced with the + • The POST method does not have any restriction on
character and any other non-alphanumeric data size to be sent.
characters are replaced with a hexadecimal values. • The POST method can be used to send ASCII as
After the information is encoded it is well as binary data.
sent to the server. • The data sent by POST method goes through HTTP
header so security depends on
The GET Method HTTP protocol. By using Secure HTTP you can make
The GET method sends the encoded user sure that your information is
information appended to the page request. The secure.
page and the encoded information are separated by • The PHP provides $_POST associative array to
the ? character. access all the sent information using
POST method.
http://www.test.com/index.htm?name1=value1&n Try out following example by putting the source
ame2=value2 code in a PHP script.

• The GET method produces a long string that


appears in your server logs, in the
browser’s Location: box.
• The GET method is restricted to send upto 1024
characters only.
• Never use GET method if you have password or
other sensitive information to be
sent to the server.
Doubles are floating-point numbers, like 3.14159 or
49.1.
Booleans have only two possible values either true
or false.
NULL is a special type that only has one value:
NULL.
Strings are sequences of characters, like ‘PHP
supports string operations.’
Arrays are named and indexed collections of other
values.
Objects are instances of programmer-defined
classes, which can package up both other kinds
The $_REQUEST variable of values and functions that are specific to the
class.
The PHP $_REQUEST variable contains the contents Resources are special variables that hold
of both $_GET, $_POST, and references to resources external to PHP (such as
$_COOKIE. database connections).
The PHP $_REQUEST variable can be used to get the
result from form data sent with both • PHP language supports following type of
the GET and POST methods. operators:
Try out following example by putting the source - Arithmetic Operators
code in a PHP script. - Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
• There are two ways the browser client can send
information to the web server.
- The GET Method
- The POST Method

Here $_SERVER[‘PHP_SELF‘] variable contains the


name of self script in which it is being MODULE 02: EXPRESSION AND CONTROL FLOW
called. LESSON 01: BASIC CONTROL STRUCTURES
It will produce the following result:
Very often when you write code, you want to perform
different actions for different conditions. You can
MODULE
use conditional statements in your code to do this.
SUMMARY
In PHP we have the following decision-making
statements:
• The PHP Hypertext Preprocessor (PHP) is a
programming language that allows web developers
• if...else statement - use this statement if you want
to create dynamic content that interacts with
to execute a set of code when a condition is true and
databases. PHP is basically used for developing
another if the condition is not true
web
• elseif statement - is used with the if...else
based software applications.
statement to execute a set of code if one of the
• The most universally effective PHP tag style is
several conditions is true
<?php...?>
• switch statement - is used if you want to select
• PHP has a total of eight data types which we use to
one of many blocks of code to be executed, use the
construct our variables
Switch statement. The switch statement is used to
Integers are whole numbers, without a decimal
avoid long blocks of if..elseif..else code.
point, like 4195.
“Have a nice Sunday!” if the current day is Sunday.
Otherwise, it will output “Have a nice day!”:

The Switch Statement


If you want to select one of many blocks of code to
The If...Else Statement be executed, use the Switch statement.
If you want to execute some code if a condition is The switch statement is used to avoid long blocks of
true and another code if a condition is if..elseif..else code.
false, use the if....else statement.
Syntax:
Syntax: switch (expression){
if (condition) case label1:
code to be executed if condition is true; code to be executed if expression = label1;
else break;
code to be executed if condition is false; case label2:
code to be executed if expression = label2;
Example. The following example will output “Have a break;
nice weekend!” if the current day is default:
Friday, Otherwise, it will output “Have a nice day!”: code to be executed
if expression is different
from both label1 and label2;
}
Example. The switch statement works in an unusual
way. First it evaluates given expression then seeks a
label to match the resulting value. If a matching
value is found then the code associated with the
matching label will be executed or if none of the
label matches then statement will execute any
specified default code.
The ElseIf Statement
If you want to execute some code if one of the
several conditions are true use the elseif
Statement

Syntax:
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

Example. The following example will output “Have a


nice weekend!” if the current day is Friday, and
The initializer is used to set the start value for the
counter of the number of loop iterations. A variable
may be declared here for this purpose and it is
traditional to name it $i.

LESSON 02: LOOPING STRUCTURE


Example. The following example makes five
Often when you write code, you want the same iterations and changes the assigned value of
block of code to run over and over again a two variables on each pass of the loop:
certain number of times. So, instead of adding
several almost equal code-lines in a script, we
can use loops.
Loops are used to execute the same block of code
again and again, as long as a certain
condition is true.
In PHP, we have the following loop types:
• for - loops through a block of code a specified
number of times. The while loop statement
• while - loops through a block of code if and as long The while statement will execute a block of code if
as a specified condition is true. and as long as a test expression is true. If the test
• do...while - loops through a block of code once, expression is true then the code block will be
and then repeats the loop as long executed. After the code has executed the test
as a special condition is true. expression will again be evaluated and the loop will
• foreach - loops through a block of code for each continue until the test expression is found to be
element in an array. false.
We will discuss about continue and break keywords Syntax:
used to control the loops execution. while (condition) {
The for loop statement code to be executed;
The for statement is used when you know how }
many times you want to execute a statement or
a block of statements.

Syntax:
for (initialization; condition; increment){
code to be executed;
}
The foreach loop statement
The foreach statement is used to loop through
arrays. For each pass the value of the current array
element is assigned to $value and the array pointer
is moved by one and in the next pass next element
will be processed.

Syntax:
foreach (array as value) {
code to be executed;
}
Example. This example decrements a variable value Example. Try out following example to list out the
on each iteration of the loop and the counter values of an array.
increments until it reaches 10 when the evaluation
is false and the loop ends.

The break statement


The PHP break keyword is used to terminate the
The do...while loop statement
execution of a loop prematurely. The break
The do...while statement will execute a block of
statement is situated inside the statement block. It
code at least once - it then will repeat the loop as
gives you full control and
long as a condition is true.
whenever you want to exit from the loop
you can come out. After coming out of a
Syntax:
loop immediate statement to the loop will be
do {
executed.
code to be executed;
}
while (condition);

Example. The following example will increment the


value of i at least once, and it will continue
incrementing the variable i as long as it has a value
of less than 1:
Example. In the following example condition test MODULE 03: ARRAYS AND FUNCTIONS
becomes true when the counter value reaches 3 LESSON 01: PHP ARRAYS
and loop terminates.
An array is a data structure that stores one or more
similar type of values in a single value.

For example if you want to store 50 numbers then


instead of defining 100 variables its easy
to define an array of 50 length.

The continue statement There are three different kind of arrays and each
The PHP continue keyword is used to halt array value is accessed using an ID c which
the current iteration of a loop but it does not is called array index.
terminate the loop.
Just like the break statement the continue • Numeric array - An array with a numeric index.
statement is situated inside the statement block Values are stored and accessed in
containing the code that the loop executes, linear fashion.
preceded by a conditional test. For the pass • Associative array - An array with strings as index.
encountering continue statement, rest of the loop This stores element values in
code is skipped and next pass starts. association with key values rather than in a strict
linear index order.
• Multidimensional array - An array containing one
or more arrays and values are
accessed using multiple indices

Numeric Array
These arrays can store numbers, strings and any
object but their index will be represented by
numbers. By default array index starts from zero.

Example. Following is the example showing how to


create and access numeric arrays.
Here we have used array() function to create array.
This function is explained in function
reference.

Example. In the following example loop prints the


value of array but for which condition
becomes true it just skip the code and next value is
printed.

Associative Arrays
The associative arrays are very similar to numeric
arrays in term of functionality but they are different
in terms of their index. Associative array will have
their index as string so that you can establish a
strong association between key and values. To store
the salaries of employees in an array, a numerically
indexed array would not be the
best choice. Instead, we could use the employees Creating PHP Function
names as the keys in our associative array, and the Its very easy to create your own PHP function.
value would be their respective salary. Suppose you want to create a PHP function which
*NOTE: Don’t keep associative array inside double will simply write a simple message on your browser
quote while printing otherwise it would not return when you will call it. Following example creates a
any value. function called writeMessage() and then calls it just
after creating it.
Note that while creating a function its name should
start with keyword function and all the PHP code
should be put inside { and } braces as shown in the
following example below

Multidimensional Arrays
A multi-dimensional array each element in the main
array can also be an array. And each element in the
sub-array can be an array, and so on. Values in the PHP Functions with Parameters
multi-dimensional array are accessed using PHP gives you option to pass your parameters inside
multiple index. a function. You can pass as many as parameters
Example. In this example we create a two- your like. These parameters work like variables
dimensional array to store marks of three students inside your function. Following example takes two
in three subjects This example is an associative integer parameters and add them together and then
array, you can create numeric array in the print them.
same fashion.

Passing Arguments by Reference


It is possible to pass arguments to functions by
reference. This means that a reference to the
variable is manipulated by the function rather than
a copy of the variable’s value.
LESSON 02:PHP USER DEFINED AND BUILT-IN
FUNCTIONS Any changes made to an argument in these cases
will change the value of the original variable. You
PHP functions are similar to other programming can pass an argument by reference by adding an
languages. A function is a piece of code ampersand (&) to the variable name in either the
which takes one more input in the form of parameter function call or the function definition.
and does some processing and returns Following example depicts both the cases.
a value.
In this lesson, there are two parts which should be
clear to you
• Creating a PHP Function
• Calling a PHP Function
Array Functions. PHP Array Functions allow you to
interact with and manipulate arrays in various ways.
PHP arrays are essential for storing, managing, and
operating on sets of variables.
PHP supports simple and multi-dimensional arrays
and may be either user created or created by
another function.
There is no installation needed to use PHP array
functions; they are part of the PHP core and comes
along with standard PHP installation.
PHP Functions returning value
Date & Time Functions. These functions allow you
A function can return a value using the return
to get the date and time from the server where your
statement in conjunction with a value or object.
PHP scripts are running. You can use these
return stops the execution of the function and sends
functions to format the date and time in many
the value back to the calling code.
different ways.
You can return more than one value from a function
Error & Logging Functions. These are functions
using return array(1,2,3,4).
dealing with error handling and logging. They allow
you to define your own error handling rules, as well
Following example takes two integer parameters
as modify the way the errors can be logged. This
and add them together and then returns their sum to
allows you to change and enhance error reporting to
the calling program. Note that return keyword is
suit your needs. Using these logging functions, you
used to return a value from a function.
can send messages directly to other machines, to
an email, to system logs, etc., so you can selectively
log and monitor the most important parts of your
applications and websites.
Dynamic Function Calls
It is possible to assign function names as strings to JSON Functions. The JSON extension implements
variables and then treat these variables exactly as the JavaScript Object Notation data-interchange
you would the function name itself. Following format. In PHP 5, the decoding is handled by a
example depicts this behavior. parser based on the JSON_ checker by Douglas
Crockford. PHP 7 has a new and improved parser
specifically written for PHP and licensed under the
PHP license.

Math Functions. The math functions can handle


values within the range of integer and float
types.

String Functions. PHP string functions are the part


of the core. There is no installation required to use
PHP Built-in Functions this function
PHP is very rich in terms of Built-in functions. In fact
you hardly need to create your own PHP function MODULE
because there are already more than 1000 of built- SUMMARY
in library functions created for different area and
you just need to call them according to your • An array is a special variable, which can hold more
requirement. than one value at a time.
Here is the list of various important function • In PHP, there are three types of arrays:
categories. There are various other function Numeric arrays - Arrays with a numeric indexed
categories which are not covered here. Associative arrays - Arrays with named keys
Multidimensional arrays - Arrays containing one or Visibility
more arrays The visibility keywords (public, protected, private)
• The real power of PHP comes from its functions. determine how an object properties/method are
PHP has more than 1000 built-in functions, and in accessible:
addition you can create your own custom functions. • public - can be accessed anywhere.
• Besides the built-in PHP functions, it is possible to • protected - can be accessed by the class and
create your own functions: subclasses only.
- A function is a block of statements that can be • private - can be accessed by the class only.
used repeatedly in a program.
- A function will not execute automatically when a A property must be defined with one of the above
page loads. visibility keywords. A method defined with- out any
- A function will be executed by a call to the function. of them will have public visibility by default.

MODULE 04: INTRODUCTION TO OOP IN PHP


LESSON 01: CLASSES AND OBJECTS

PHP supports OOPS programming paradigm which


bundles data (properties) and related functions
(methods) in independent units called objects.

A class is a blueprint from which we create object


instances. A class mainly contains properties
and methods. Variables and functions are called
properties and methods respectively in OOP
world.
Class Constants. Just like defining other constants
To name the class, it is customary to use a singular (outside of a class) using ‘const’ keyword, we can
noun that starts with a capital letter. For example, also define them inside a class. The default visibility
we can group a code that handles users into a User of class constants is public. Constants are
class, the code that handles posts into a Post class, allocated once per class, and not for each class
and the code that is devoted to comments into a instance.
Comment class.
Scope Resolution Operator (::). Instead of using ->,
double colon allows access to static and constant.
This operator is also used to access super class
features.

Using ‘self’. Instead of using $this, the self-keyword


is used to access constants within the class.
Generally, for all class level access self should be
‘new’ keyword. To create an instance of a class, used and for all current object instance access
new keyword is used new ClassName(); $this should be used within the class..

pseudo-variable $this. The reference to the current


object, only used within the class.

Object operator ->. Used when calling a method or


accessing a property on an object in- stance. It is
used with $this as well.
Relationship between classes and objects PHP Constructor. Let’s take the example of a class
While in the procedural style of programming, all of Car which has two properties, make and color, for
the functions and variables sit together in the global this class we will define a constructor for initializing
scope in a way that allows their use just by calling the class properties(variables) at the time of object
their name, the use of classes makes anything creation
inside the classes hidden from the global scope.
That’s because the code inside the classes is PHP Destructor. PHP Destructor method is called
encapsulated within the class scope, outside the just before PHP is about to release any object from
reach of the global scope. So, we need a way to its memory. Generally, you can close files, clean up
allow the code from the global scope to use the resources etc. in the destructor method. Let’s take
code within the class, and we do this by creating an example:
objects from a class.

LESSON 02: CONSTRUCTORS AND


DESCTRUCTORS
As we can see in the output above, as the PHP
program ends, just before the PHP initiates release
When we create an object of any class, we need to
of the object created, and hence the destructor
set properties of that object before using it. We can
method is called.
do that by first initializing the object and then setting
The destructor method cannot accept any argument
values for the properties, either by using the ->
and is called just before the object is deleted, which
operator if the variables are public, or using the
happens either when no reference exists for an
public setter methods for the private variables.
object or when the PHP script finishes its execution.
To create and initialize a class object in a single
If you see a PHP class with a function having same
step, PHP provides a special method called as
name as the name of the class, then that function
Constructor, which is used to construct the object
will act as the constructor. In older versions of PHP,
by assigning the required property values while
constructor was not defined using the __construct ()
creating the object. And for destroying the object
name, but it used to have the same name as the
Destructor method is used.
class name just like Core Java.

Constructor can accept arguments, whereas


destructors won’t have any argument because a
destructor’s job is to destroy the current object
reference.
ITELEC3: Object-Oriented Programming

Short Review
Which keyword is used A. Function

to declare a class in B. Class


C. Interface
PHP? D. Object

10/9/2023 2
Which keyword is used A. Function

to declare a class in B. Class


C. Interface
PHP? D. Object

10/9/2023 3
You are designing a PHP A. Private
application with multiple B. Protected
classes, and you want to control C. Public
the visibility of certain class D. Static
properties and methods. To
achieve this, you decide to use
access modifiers. Which access
modifier should you choose to
restrict a class property or
method to be accessible only
within its own class and derived
classes?
10/9/2023 4
You are designing a PHP A. Private
application with multiple B. Protected
classes, and you want to control C. Public
the visibility of certain class D. Static
properties and methods. To
achieve this, you decide to use
access modifiers. Which access
modifier should you choose to
restrict a class property or
method to be accessible only
within its own class and derived
classes?
10/9/2023 5
You’re debugging a PHP A. Current instance of the
codebase, and you encounter class
the ‘this’ keyword within a class. B. Static methods of the
Your team member asks you to class
clarify its reference. In this C. Parent class
scenario, what does the ‘this’ D. Abstract class
keyword typically refer to within
a class?

10/9/2023 6
You’re debugging a PHP A. Current instance of the
codebase, and you encounter class
the ‘this’ keyword within a class. B. Static methods of the
Your team member asks you to class
clarify its reference. In this C. Parent class
scenario, what does the ‘this’ D. Abstract class
keyword typically refer to within
a class?

10/9/2023 7
What is the output of the sample A. The fruit is Apple.
program?
B. The Fruit is Apple.
C. Fatal error: Class Fruit
cannot extend final class
Fruit2 in
C\xampp\htdocs\ex\loop
2.php on line 13
D. None of the choices

10/9/2023 8
What is the output of the sample A. The fruit is Apple.
program?
B. The Fruit is Apple.
C. Fatal error: Class Fruit
cannot extend final class
Fruit2 in
C\xampp\htdocs\ex\loop
2.php on line 13
D. None of the choices

10/9/2023 9
You are working on a PHP A. $date =
project where you need to date_create(‘now’);
manipulate and display dates.
B. $date = date_create(“m-
You want to create a DateTime d-Y”);
object to represent a specific
C. $date =
date and time. Which of the date_create(date(“m/d/Y
following options demonstrates ”));
the correct usage of date_create
D. $date = date_create();
to get the present datetime?

10/9/2023 10
You are working on a PHP A. $date =
project where you need to date_create(‘now’);
manipulate and display dates.
B. $date = date_create(“m-
You want to create a DateTime d-Y”);
object to represent a specific
C. $date =
date and time. Which of the date_create(date(“m/d/Y
following options demonstrates ”));
the correct usage of date_create
D. $date = date_create();
to get the present datetime?

10/9/2023 11
You have created a class named
User with variables: username A. $user1->username=“cliffsama”;
and password set as private, with $user2->username=“rebansome”;
getter/setter methods. You $user1->password=“mypeasants”;
instantiated the class as objects
named user1 and user2. Which $user2->password=“kneelbeforeme”;
of the following options B. $username=$this->username;
represents the correct syntax for
$password=$this->password;
setting the value of the variables
of the class inside the methods? C. $this->username=$username;
$this->password=$password;
D. $user1->set_username(“kneelbeforeme”);
$user2->set_username(“mypeasants”);
$user1->set_password(“mypeasants”);
$user2->set_password(“kneelbeforeme”);

10/9/2023 12
You have created a class named
User with variables: username A. $user1->username=“cliffsama”;
and password set as private, with $user2->username=“rebansome”;
getter/setter methods. You $user1->password=“mypeasants”;
instantiated the class as objects
named user1 and user2. Which $user2->password=“kneelbeforeme”;
of the following options B. $username=$this->username;
represents the correct syntax for
$password=$this->password;
setting the value of the variables
of the class inside the methods? C. $this->username=$username;
$this->password=$password;
D. $user1->set_username(“kneelbeforeme”);
$user2->set_username(“mypeasants”);
$user1->set_password(“mypeasants”);
$user2->set_password(“kneelbeforeme”);

10/9/2023 13
Sample Footer Text

Given the code, what will be the output?


A. C.

* *****

** ****

*** ***

**** **

***** *

B. D.error

*****

*****

***

**

10/9/2023 14
Sample Footer Text

Given the code, what will be the output?


A. C.

* *****

** ****

*** ***

**** **

***** *

B. D.error

*****

*****

***

**

10/9/2023 15
A ______ is a way to A. $_SESSION
store information (in B. $_POST
variables) to be used C. $_REQUEST
across multiple pages. D. $_GET

10/9/2023 16
A ______ is a way to A. $_SESSION
store information (in B. $_POST
variables) to be used C. $_REQUEST
across multiple pages. D. $_GET

10/9/2023 17
A _____ statement A. Printf
can be used to get B. Echo
output and returns a C. Write
value of 1. D. print

10/9/2023 18
A _____ statement A. Printf
can be used to get B. Echo
output and returns a C. Write
value of 1. D. print

10/9/2023 19
What will be printed A. Apple banana mango grape
to the screen when B. Grape mango banana apple
executing the code C. Banna apple grape mango
snippets below? D. Mango banana grape apple

$fruit=[“apple”, “banana”,
“mango”, “grape”];

foreeach($fruits as $fruit){
echo $fruit . “ “;
}

10/9/2023 20
What will be printed A. Apple banana mango grape
to the screen when B. Grape mango banana apple
executing the code C. Banna apple grape mango
snippets below? D. Mango banana grape apple

$fruit=[“apple”, “banana”,
“mango”, “grape”];

foreeach($fruits as $fruit){
echo $fruit . “ “;
}

10/9/2023 21

You might also like