Day 8: PHP Language reference and JS Basics

Module: Programming Languages
Topics: HTML & CSS, PHP & MYSQL and JavaScript

PHP Classes and Object

Php includes OOP concepts as well some of them are Class Abstraction, Object Inheritance, and more.

Object Inheritance

Just like we see the importance of Interheretance in other languages like cpp. PHP also uses these principles in its object class.

We can extend a class and the subclass can inherit all the public and protected methods, property, and constant from the parent class. Unless a class overrides those methods, they will retain their original functionality.

Also Private Methods of the parent class cannot be accessed in a child class.

Before PHP 8.0.0, final and static restrictions were applied to private methods

After PHP 8.0.0, only private final constructors are private.

Readonly modifier introduced in PHP 8.1.0, it can be assigned to a property, which prevents future modification.

<?php

class A {
    public int $prop;
}
class B extends A {
    // Illegal: read-write -> readonly
    public readonly int $prop;
}
?>

Scope Resolution Operator (::)

It is a token that allows access to the constantstatic property, or static method of a class or one of its parents.

To reference these items from the outside of the class definition then we can use the name of the class.

NOTE: There are three keywords self, parent, and static are used to access properties or methods from inside the class definition.

// From inside the Class Defination function 
echo parent::CONST_VALUE . "\n";
echo self::$my_static . "\n";

// From outside the Class Defination
$classname = 'ClassName';
$classname::doubleColon();
// OR
ClassName::doubleColon();
?>

PHP Traits

It provides a way to reuse the code again. It allows code reuse in single inheritance languages like PHP. It is similar to a class but only intended to group functionality in a fine-grained and consistent way.

<?php
trait demoTraitInfo {
    function getReturnType() { } 
}

class ezcReflectionMethod extends ReflectionMethod {
    use demoTraitInfo;
    /* ... */
}
?>

SASS: A Stylesheet Language

SASS (Syntactically Awesome Stylesheets) is a stylesheet language that can be compiled into CSS. It offers and even extends the capabilities of the CSS. It has many features like variables, nesting, and mixins.

SAAS variables are completely different than the variables we have in the CSS.

We can also use the!default tag provided by the saas. We can use this in our library before we generate the CSS. Using this we can assign value to a variable only if the variable is NULL or undefined.

//SAAS
$base-color: #c6538c
$border-dark: rgba($base-color, 0.88)

.alert
  border: 1px solid $border-dark
/** CSS **/
.alert {
  border: 1px solid rgba(198, 83, 140, 0.88);
}

CSS: Display Property

block:

  • The element is displayed as a block-level element, taking up the full width of its container. It starts on a new line and stretches the entire width.

inline:

  • The element is displayed as an inline-level element, allowing it to flow within the content. It does not start on a new line, and its width and height are only as much as necessary.

inline-block:

  • The element is displayed as an inline-level element but behaves as a block-level element in terms of layout. It allows for setting width and height.

flex:

  • The element becomes a flex container, and child elements become flex items. This allows for easy manipulation of the layout using flex properties.

grid:

  • The element becomes a grid container, and child elements become grid items. This allows for creating complex layouts using the CSS Grid system.

Javascript

Closure

  • A closure is created when a function is defined inside another function, allowing the inner function to access the outer function’s variables, parameters, and even the outer function’s scope chain.
  • Closures are powerful for creating private variables and encapsulating functionality.
function outerFunction() {
  let outerVariable = 10;

  function innerFunction() {
    console.log(outerVariable);
  }

  return innerFunction;
}

const closure = outerFunction();
closure(); // Outputs: 10

Lexical Scope

  • Lexical scope, also known as static scope, is a scope resolution mechanism where the scope of a variable is determined by its location in the source code during the lexical analysis phase.
  • This means that the scope of a variable is fixed at the time of its definition and is based on the structure of the code.
function outer() {
  let outerVariable = 'I am outer!';

  function inner() {
    console.log(outerVariable);
  }

  inner();
}

outer(); // Outputs: I am outer!

Let Vs Var

They both are used to declare variables, but they have some differences in terms of scope and hoisting.

  1. Scope:
    • var has function scope. This means that a variable declared with var is only available within the function it is declared in.
    • let has block scope. It is limited to the block, statement, or expression where it is defined.
  2. Hoisting:
    • Variables declared with var are hoisted to the top of their scope. This means that you can use the variable before it’s declared in the code, but its value will be undefined.
    • Variables declared with let are also hoisted, but they are not initialized until the interpreter reaches the declaration in the code. Accessing a let variable before its declaration results in a ReferenceError.

Example:

// Example with var
function exampleVar() {
  console.log(x); // undefined
  var x = 5;
  console.log(x); // 5
}

exampleVar();

// Example with let
function exampleLet() {
  // console.log(y); // ReferenceError: Cannot access 'y' before initialization
  let y = 10;
  console.log(y); // 10
}

exampleLet();

“Good Work. Good People.”

Leave a Reply

Your email address will not be published. Required fields are marked *