Skip to content

Instantly share code, notes, and snippets.

@prasanth22
Last active December 27, 2020 16:10
Show Gist options
  • Save prasanth22/728f74c9d0ec03ae6603391c15b6b6f5 to your computer and use it in GitHub Desktop.
Save prasanth22/728f74c9d0ec03ae6603391c15b6b6f5 to your computer and use it in GitHub Desktop.

Functions:

//function declararion
function name($firstName, $lastName = 'defaultvalue') {
    puts $firstName . ' ' . $lastName
}

//function call
name('Mike', 'Taylor');

//function call with named parameters (PHP 8)
name(firstName: 'Mike', lastName: 'Taylor'); // order can change

//function variables params
function name(...$params) {
    return $params[0] . “ “ . params[1];
}

// Closure function
Route::get('/', function () {
     return view('welcome');
});

// Arrow functions
Route::get('/', fn () => view('welcome');

Oops:

//class declaration
class Person 
{
}

// object instantiation
$person = new Person

//class properties and constructor
class Person 
{
   protected $firstName;
   protected $lastName;
   public function __construct($firstName, $lastName) {
        $this->firstName = $firstName;
        $this->lastName = $lastName
   }

// Constructor Property Promotion (PHP 8)
class Person 
{
    public function __construct(protected $firstName, protected $lastName) 
    {

    }

//static constructor
public static function create(...$params) {
    return new self($params)
}
$person = Person::create(‘Mike’, ‘Taylor’);

// class inheritance
class Customer extends Person
{
    public function name()
    {
        parent::name();
        echo 'Override method';  
    }
}

// Static Method
class greeting {
  public static function welcome() {
    echo "Hello World!";
  }
}

// Call static method
greeting::welcome();

// Static method internal call
class greeting {
  public static function welcome() {
    echo "Hello World!";
  }

  public function __construct() {
    self::welcome();
  }
}
new greeting();

// Interface
interface Animal {
  public function makeSound();
}

class Cat implements Animal {
  public function makeSound() {
    echo "Meow";
  }
}
$animal = new Cat();
$animal->makeSound();

//Trait (mix-in)
trait HelloWorld {
    public function sayHello() {
        echo 'Hello World!';
    }
}

class Greetings {
    use HelloWorld;
}
$object = new Greetings();
$object->sayHello();

php

Associative Arrays:
index.php

<?php 
$person = [
	'age' => 31,
	'hair' => 'brown,
	'career' => 'web developer'
];

$person['name'] = 'prasanth'; // add the value to the associative array

unset($person['hair']); // remove the value from the associative array

require(index.blade.php);
?>

index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <ul>
        <?php foreach ($person as $key => $value): ?>
        <li><strong><?= $key; ?></strong><?= $value; ?> </li>
        <?php endforeach; ?>
    </ul>
</body>
</html>

Functions:

<?php

function dd($data){
    echo "<pre>";
    die(var_dump($data));
    echo "</pre>";
}

?>

Classes:

class Task {
    protected $description;
    protected $completed = false;
    public function __construct($description){
        $this->description = $description;
    }
    public function isCompleted(){
        return $this->completed;
    }
}

$task = new Task('go to store');

var_dump($task->isCompleted());//bool(false)

another example:

index.php
<?php
class Task {
    public $description;
    protected $completed = false;
    public function __construct($description){
        $this->description = $description;
    }
    public function isCompleted(){
        return $this->completed;
    }

    public function complete(){
     $this->completed = true;
    }
}

$tasks = [
    new Task('go to store'),
    new Task('go to market'),
    new Task('read book')
];

$tasks[1]->complete();

//var_dump($tasks); //bool(false)
require 'index.blade.php';
?>

index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <ul>
        <?php foreach ($tasks as $task): ?>
            <?php if ($task->isCompleted()): ?>
                <strike><li><?= $task->description; ?></li></strike>
            <?php else: ?>
                <li><?= $task->description; ?></li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>
</body>
</html>

output:

  • go to store
  • go to market
  • read book

php Routing

  • Request header
      GET /about HTTP/1.1
      Host: phpden.info
    
  • Response
    HTTP/1.1 200 ok
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment