Internet Computing T2

0.0(0)
studied byStudied by 4 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/140

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

141 Terms

1
New cards

Does OOP overloading apply in PHP

No, PHP is not a compiling language

2
New cards

How does PHP implement overloading

uses variable-length arguments (also called variadic functions) to accept flexible number of arguments

<p>uses <strong>variable-length arguments</strong> (also called variadic functions) to accept flexible number of arguments</p><p></p><p></p>
3
New cards

How does overrriding work in PHP

same as OOP

<p>same as OOP</p>
4
New cards

How to override inherited methods

by redefiining methods using same name (overriding) in child class and with the final keyword

<p>by redefiining methods using same name (overriding) in child class and with the <strong>final</strong> keyword</p>
5
New cards

Abstract classes vs methods

class: contains at least one abs. method

method: method that is declared but not implemented

6
New cards

Abstract Classes Rules

  • child class methods must be defined with same name

  • child class method must be defined with same or less restricted access

  • number of required arguments must be the same, but child class can have optional arguments aditionally

7
New cards

What are interfaces

defined in same way as a class, but says interface instead of class.

to implement interface, class needs to use implements keyword

8
New cards

Interface vs. Abstract Classes

  • interfaces cannot have properties, but abstract classes can

  • all interface methods must be public, while abstract classes can be public or protected

  • all methods in an interface are abstract, so abstract keyword is not necessary, but all methods in an abstract class do not have to be abstract

  • classes can implement an interface while inheriting from another class at the same time.

9
New cards

Methods that connect PHP to MySQL

MySQLi

PDO

difference: PDO suppords various databases and MySQLi supports only MySQL. MySQLi also a bit faster. PDO supports 12 different drivers, opposed to MySQLi (MySQL only)

10
New cards

MySQLi code demo

knowt flashcard image
11
New cards

PDO code demo

knowt flashcard image
12
New cards

create DB with MySQLi

knowt flashcard image
13
New cards

Create DB with PDO

knowt flashcard image
14
New cards

Create Table MySQLi

knowt flashcard image
15
New cards

Create Table PDO

knowt flashcard image
16
New cards

MySQLi Insert

knowt flashcard image
17
New cards

PDO Insert

knowt flashcard image
18
New cards

Prepared Statement MySQLi vs. PDO

knowt flashcard image
19
New cards

prepared statements procedures

  • prepare sql query with empty vals. as placeholders with “?” or variable name (ex. “INSERT INTO MyGuests VALUES(?, ?, ?)”)

  • bind values or variables to placeholders

  • execute query simultaneously

20
New cards

MySQLi prepared statement procedure

knowt flashcard image
21
New cards

benefits of using SQL prepared statments

  • useful against SQLi attacks

  • reduce parsing time since preparation is done only once

  • minimizes bandwidth to server since you need only params each time

22
New cards

PDO prepared statement procedure

knowt flashcard image
23
New cards

PDO vs. MySQLi

knowt flashcard image
24
New cards

What is PDO

an extension that defines lightweight consistent interface for accessing databases in PHP

25
New cards

What classes does PDO contain?

  • PDO class: represents connection between PHP and DB server

  • PDOStatement class: represents prepared statement and, after statement is exectured, an associated result test

  • PDOException class: represents error rasied by PDO (dont raise from ur own code)

26
New cards

Types of prepared statments

  • named parameters

  • positional parameters

27
New cards

when should you use prepared statments

  • SQL statements contain user inputs or SQL statments run repeatedly

28
New cards

When should you use direct execution APIs

SQL statements do not contain user inputs and SQL statments run one time

29
New cards

How to use direct execution (PDO statment)

$pdo→exec(…)

30
New cards

Steps for PDO prepared statement

  • two param methods (named param, positional param)

    1. $pdo→prepare(…)

    2. $pdo→bindParam(…)
      $pdo→bindValue(…)

    3. $pdo→execute(…)

31
New cards

SQL templates for named parameters and positional parameters

  • named parameters: the form :name

  • positional parameters: ?

32
New cards

PDO prepared syntax

PDO::prepare(string $query, array $options = []): PDOStatement|false

  • $query: a valid SQL statement template

  • $option: set attribute values for PDOSStatement object

33
New cards

bindValue() function

PDO::bindValue(string|int $param, mixed $value, int $type = PDO::PARAM_STR): bool

  • $param: for prep. statment with named placeholder will be in form :name , ? placeholder statements will be the 1-index position of param

ex

  • Example:

    $stmt1 -> bindValue (':name ','Ben’, PDO :: PARAM_STR );

    $email = ‘shaun@gmail.com';

    $stmt1 -> bindValue (':email ',$email );

34
New cards

bindParam() function

35
New cards

bindParam()

bool PDO::bindParam(string|int $param, mixed &$var, int $type = PDO::PARAM_STR, int $maxLength = 0, mixed $driverOptions = null): bool

  • ex

    • $name = 'Ben ';

      $stmt1 -> bindParam (':name’, $name ,PDO::PARAM_STR );

36
New cards

named parameter example

knowt flashcard image
37
New cards

positional parameter example

knowt flashcard image
38
New cards

difference between bindValue() and bindParam()

  • similar from user perspective, differ in implementation

  • bindParam() binds param exclusively to specified variable name, bound as reference

  • bindValue() binds value that could be a varaible, integer or string to parameter

39
New cards

Debug tools for PHP

  • visual studio

  • Xdebud

40
New cards

Debug techniques

  • dump variables to standard output (stdout)

  • switching error reporting level - update php.ini

41
New cards

main uses of PHP

  • server side scripting

  • requires: PHP parser, web server, web browser

  • PHP code not visible to browser users

42
New cards

how to define variables and check data types in PHP

variables start with $, case sensitive

var_dump() checks data types

43
New cards

Method for data type conversion

Settyype(mixed &$var, string $type)

<?php
$count = "5";
echo gettype($count); // before datatype conversion: $count is a string
settype($count,'int’);
echo gettype($count); // after datatype conversion: $count is an integer
?>

44
New cards

Comparision Operators in PHP

Operator

Name

Example

Explanation

==

Equal

$a == $b

Returns true if $a is equal to $b.

===

Identical

$a === $b

Returns true if $a is equal to $b and they are of the same type.

!=

Not equal

$a != $b

Returns true if $a is not equal to $b.

<>

Not equal

$a <> $b

Returns true if $a is not equal to $b.

!==

Not identical

$a !== $b

Returns true if $a is not equal to $b or they are not of the same type.

<

Less than

$a < $b

Returns true if $a is strictly less than $b.

>

Greater than

$a > $b

Returns true if $a is strictly greater than $b.

>=

Greater than or equal to

$a >= $b

Returns true if $a is greater than or equal to $b.

<=

Less than or equal to

$a <= $b

Returns true if $a is less than or equal to $b.

<=>

Spaceship

$a <=> $b

Returns an integer less than, equal to, or greater than zero, depending on whether $a is less than, equal to, or greater than $b.

45
New cards

how do Arrays in PHP work

  • indexed array

  • associative array (paired key → value)

46
New cards

accessing items in array with foreach

syntax: foreach ($array as $key => &$value)

47
New cards
<p><span>If we remove the sign “&amp;” in foreach, what the output will be? </span></p>

If we remove the sign “&” in foreach, what the output will be?

values inside array will not be modified bc $value is just a copy of each elemt, not reference to original element

48
New cards

PHP variable scopes

  • local

  • global

  • static

49
New cards

PHP anon function

$fn = function (int $x, int $y)

  • do not inherit variables from parent scope → need to use use keyword

  • can be assigned to variables, passed as arguments to other functions, or returned from functions.

$factor = 10;
$multiplier = function($x) use ($factor) {
    return $x * $factor;
};
echo $multiplier(5); // Outputs: 50

50
New cards

PHP arrow function

$fn = fn (int $x, int $y)

  • automatically inherit variables from parent scope

  • used for simpler one liner functions

$factor = 10;
$multiplier = fn($x) => $x * $factor;
echo $multiplier(5); // Outputs: 50

51
New cards

function declaration

declare(strict_types=1)

52
New cards

how to accept variable number of arguments

function functionName(...$args) { ... }

53
New cards

superglobal variables

  • associative arrays that are always accessible, regardless of scope

  • provide information about server, request, and environment

54
New cards

nodejs

  • open source back-end javascript runtime environtment

  • allows developers to use javascript for server side scripting

  • event driven architecture capable of asynchronour I/O

55
New cards

how to start javascript code

<script>…</script>

56
New cards

is JS case sensitive

yes

57
New cards

how do JS comments work

  • supports both C and C++ style comments

  • any text between // and at end of line is treated as comment

  • any text between /* and */ is treated as a comment

58
New cards

how is JS data type determined

dynamicaly based on value stored

59
New cards

Difference between function_name and function_name() in
JavaScript

function_name refers to the function object while function_name() refers to

the function execution result.

60
New cards

display function exec results example

knowt flashcard image
61
New cards

scopes in javascript

  • block scope: decalred inside {} block with either let or const cannot be accessed outside the block

  • function/local scope

  • global scope

62
New cards

variable scope diference between PHP and JS?

63
New cards

ways to declare a javascript varaible

  • var - function scope

  • let - cannot be redeclared afterwards, have block scope

  • const - cannot be chaned after assigned valueble scope difference between PHP and JS?


64
New cards

what are identifiers in JS

unique names JS variables are identified by

65
New cards

JS hoisting

  • varaibles defined with var are hoiseted to the top anc can be initialized at any time

  • with let you cannot use a varaible before its declared

66
New cards

JS functions to extract string parts

  1. slice(start, end) - start, end are indexes

  2. substring(start, end) - does not accept negative indices

    • if start > end, substring will swap arguments

  3. substr(start, end) → DEPREACTED

67
New cards

how to replace parts of string in JS

replace(searchValue, newValue)

68
New cards

What kind of number does JS have

64-bit floating point

69
New cards

Symbol in JS

  • represents unique identifier

  • ever symbol call is guaranteed to return a unique symbol

70
New cards

how to create an object in JS

  1. using Object Literal/Initializer Syntax

    var p1 = { name:“John" }; // object literal syntax

  2. using new operator

    var p2 = new Car(‘BMW’, ‘red’); //Car can be either a function or a class
    such that an object

  3. using the Object() Constructor function with new keyword or create()

    var p2 = Object.create(car); // Object() constructor function

    p2.name = “John"; // property

71
New cards

JS object creation limitation

a property name that has a space or a hyphen, or that starts with a
number) can only be accessed using the square bracket notation.

72
New cards

what is a JS object property and types of properties

characteristic of an object, often describing attributes associated with data structure

  1. instance properties: hold data specific to object instance

  2. static properties: hold data shared among all other instances

73
New cards

what does a property have in JS

a name (string or symbol)

a value (primitive, method, or object reference)

74
New cards

how to access object properties JS

  • objectName.property

    • ex. person.age

  • objectName[“property”]

    • ex. person[“age”]

  • objectName[expression]

    • ex. x=”age”; person[x]

75
New cards

JS methods

JS methods can be located at value location in key:value

JS method is property containing function definition

76
New cards

how to access object methods JS

objectName.methodName()

77
New cards

JS accessors

gettters: get property()

setters: object.property = “x”

78
New cards

JS class

program code template for creating objects

class ClassName {
constructor() { ... }
method_1() { ... }
method_2() { ... }
...
}
//"constructor“ is a special method
// constructor does not have a name
// there is no destructor() in JavaScript

79
New cards

JS class example

knowt flashcard image
80
New cards

JS inheritance

uses extends, inhetits all methods from parent class

super() method in constructor meands parent property is called

81
New cards

JS encapsulation

  • public/private

  • by default all methods/vars are public

  • private members can be created using # prefix

<ul><li><p>public/private</p></li><li><p>by default all methods/vars are public</p></li><li><p>private members can be created using <strong>#</strong> prefix</p><p></p></li></ul><p></p>
82
New cards

getter and setter syntax

{get prop() { return property; }}

{set prop(val) { ... }}

  • Do not use parentheses when calling a getter (e.g., rect.area, not rect.area()).

  • get and set can be used both inside and outside of classes

83
New cards

getter example

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  get area() {
    return this.height * this.width;
  }
}
const rect = new Rectangle(5, 10);
console.log(rect.area); // Outputs: 50

84
New cards

setter examples

const language = {
  set current(name) {
    this.log.push(name);
  },
  log: []
};
language.current = 'en';
language.current = 'jp';
console.log(language.log); // Outputs: ['en', 'jp']

85
New cards

this keyword in JS vs PHP

JS:

  • this refers to object that is executing in curr function

  • in global scope, refers to global object

  • in functions, refers to hlobal object or undefined (nonstrict vs. strict)

  • in methods, refers to object that owens the method

  • in event handlers, refers to element that recieved event

  • in arrow functions, inherited from parent scope

PHP

  • this is used within class methods tp refer to current object instance

  • used to access properties and methods of class

  • cannot be used in static methods, since they are called on class itself and not instance

  • not used in global scope

86
New cards

function declaration vs function expression

declaration:

  • function declarations load before any code is executed,

  • hoisted on top of other code,

  • cannot define anon func

expresson:

  • function expressions load only when the interpreter reaches that line of code,

  • not hoisted,

  • can define anon func

<p><strong>declaration:</strong> </p><ul><li><p>function declarations load before any code is executed, </p></li><li><p>hoisted on top of other code, </p></li><li><p>cannot define anon func</p></li></ul><p><strong>expresson:</strong></p><ul><li><p>function expressions load only when the interpreter reaches that line of code, </p></li><li><p>not hoisted, </p></li><li><p>can define anon func</p></li></ul><p></p>
87
New cards

What are the basic syntax forms of JavaScript arrow functions?

param => expression

//ex
param => {
    let a = 1;
    return a + param;
}

(param1, paramN) => expression

//ex
(param1, paramN) => {
    let a = 1;
    return a + param1 + paramN;
}

88
New cards

arrow functions limitations

  • does not have its own bindings to this or super and should not be used as methods

  • does not have new.target keyword

  • noy suitanle for call, applu, and bind methods which rely on scope

  • cannot be used as constructors

89
New cards

What are the key differences between traditional functions and arrow functions?

  • Traditional functions use the function keyword, while arrow functions use =>.

  • Arrow functions are more concise, especially for single-expression functions.

  • Traditional functions are more versatile (e.g., can be used as constructors), while arrow functions are ideal for short, non-method functions.

90
New cards

what do JS function parameters not do

  • function definitions do not specify data types for parameters.

  • functions do not perform type checking on the passed arguments.

  • functions do not check the number of arguments received ←→ PHP

91
New cards

what are the arguments object in JS functions

array of arguments used when function called

92
New cards

default parameters in JS

if function is called with missing arguments, the missing valuesa re set to undefined and JS keeps running

93
New cards

how to invoke a function as a method of an object

objName.methodName()

ex. console.log(employee.fullName());

94
New cards

how to invoke function with a function constructor

new Function(functionBody)

new Function(arg1,…argN, functionBody)

95
New cards

call() method JS

predefined method of function objects

useful when you want to reuse a function with different objects or explicitly set value of this

  • function is not a method of object you want to use with

  • want to borrow method from one object and use with another object

96
New cards

call() method example

const person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

const person1 = {
  firstName: "John",
  lastName: "Doe"
};

// Using call() to set `this` to person1
console.log(person.fullName.call(person1)); // Outputs: John Doe

Here, fullName is a method of the person object, but we want to use it with person1. By using call(), we explicitly set this to person1, allowing the function to access person1's properties (firstName and lastName).

97
New cards

when to use call()

  1. borrowing methods

    const person2 = {
      firstName: "Jane",
      lastName: "Smith"
    };
    console.log(person.fullName.call(person2)); // Outputs: Jane Smith
  2. explicitly setting this

    function greet() {
      console.log("Hello, " + this.name);
    }
    
    const user = { name: "Alice" };
    greet.call(user); // Outputs: Hello, Alice

98
New cards

When is call() necessary in JavaScript, and when is it unnecessary?

  • Unnecessary:
    When a function is already a method of the object you're calling it on. Use object.method() directly.
    Example: employee.fullName().

  • Necessary:
    When you want to reuse a function with a different object or explicitly set this. Use function.call(thisArg).
    Example: person.fullName.call(person1).

99
New cards

JS apply() function

  • The apply() method is similar to call(), but it allows you to pass arguments to the function as an array (or array-like object).

  • It is used to invoke a function with a specific this value and arguments provided as an array.

synatx: functionName.apply(thisArg, [argsArray]);

100
New cards

JS bind() function

  • The bind() method creates a new function with the same body as the original function but with a fixed this value.

  • Unlike call() and apply(), bind() does not immediately invoke the function. Instead, it returns a new function that can be called later.