Open theachoem opened 3 years ago
class Fruit {
// Properties
public $name;
// Method
function set_name($name) {
$this->name = $name;
}
}
$banana = new Fruit();
$apple->set_name('Apple');
You can use the instanceof keyword to check if an object belongs to a specific class:
<?php $apple = new Fruit(); var_dump($apple instanceof Fruit); //result: bool(true) ?>
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
There are three access modifiers:
1. public - the property or method can be accessed from everywhere. This is default
2. protected - the property or method can be accessed within the class and by classes derived from that class
3. private - the property or method can ONLY be accessed within the class
class Strawberry extends Fruit {
//statement
}
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}
echo Goodbye::LEAVING_MESSAGE;
we can access a constant from inside the class by using the self keyword.
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
public function byebye() {
echo self::LEAVING_MESSAGE;
}
}
$goodbye = new Goodbye();
$goodbye->byebye();
// Parent class
abstract class Car {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function intro() : string;
}
class Audi extends Car {
public function intro() : string {
return "Choose German quality! I'm an $this->name!";
}
}
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
echo "Meow";
}
}
$animal = new Cat();
$animal->makeSound();
Interface are similar to abstract classes. The difference between interfaces and abstract classes are:
- Interfaces cannot have properties, while abstract classes can
- All interface methods must be public, while abstract class methods is public or protected
- All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary
- Classes can implement an interface while inheriting from another class at the same time
Static methods can be called directly - without creating an instance of the class first.
ClassName::staticMethod();
XI. Static properties
Static properties can be called directly - without creating an instance of a class.
ClassName::staticProp;
XII. Namespace
Namespaces are qualifiers that solve two different problems:
```php
namespace Html;
class Table {
public $title = "";
public $numRows = 0;
public function message() {
echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>";
}
}
//usage $table = new Html\Table() //or use Html as H; $table = new H\Table() //or use Html\Table as T; $table = new T();
- [Iterable](https://www.w3schools.com/php/php_iterables.asp)
- git config
- git init
- git clone
- git add
- git commit
- git diff
- git reset
- git status
- git rm
- git log
- git show
- git tag
- git branch
- git checkout
- git merge
- git remote
- git push
- git pull
- git stash
$ git init
$ git status
$ git add .
$ git commit -m "First commit"
$ git config --global user.name theacheng
$ git config user.name //theacheng
$ git config --global user.email theacheng.g6@gmail.com
$ git config user.email //theacheng.g6@gmail.com
$ git init
$ git remote add origin https://github.com/theacheng/profile_templete.git
$ git add .
$ git commit -m "commit message"
$ git pull origin master //pull last update code
$ git push origin master
$ git clone https://github.com/theacheng/profile_templete.git
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. — Composer Introduction.
Reference: https://code.tutsplus.com Official docs here: https://getcomposer.org/doc/03-cli.md#init
$ composer require phpmailer/phpmailer
To use install
, you need to create composer.json, or just run: composer init
.
In the composer.json file, you just need to declare your project dependencies, as shown in the following snippet.
{
"require": {
"phpmailer/phpmailer": "~6.1"
}
}
Anyway, the require
command will add a package to your composer.json
file automatically.
$composer require phpmailer/phpmailer
Reference: https://laravel.com/docs/5.1/quickstart#installation
$ composer create-project laravel/laravel quickstart --prefer-dist
For existing project:
$ composer install
$ php artisan migrate
To generate a migration file:
$ php artisan make:migration create_tasks_table --create=tasks
It will be placed in database/migrations
directory.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration{
// Run the migrations.
public function up(){
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
// Reverse the migrations.
public function down(){
Schema::drop('tasks');
}
}
To generate a model:
php artisan make:model Task
It will be placed in app
directory.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model {
//
}
app/Http/routes.php
file:
<?php
use App\Task;
use Illuminate\Http\Request;
Route::get('/', function () { // });
Route::post('/task', function (Request $request) { // });
Route::delete('/task/{id}', function ($id) { // });
### VIII. Displaying A View
```php
Route::get('/', function () {
return view('tasks');
});
resources/views/layouts/app.blade.php.
<!DOCTYPE html> <html lang="en"> <body> <div class="container"> <nav class="navbar navbar-default"> <!-- Navbar Contents --> </nav> </div> @yield('content') </body> </html>
X. Define a child view
resources/views/tasks.blade.php.
@extends('layouts.app')
@section('content')
<div class="panel-body">
//note that we have add element to show when error here.
@include('common.errors')
<form action="/task" method="POST" class="form-horizontal">
{{ csrf_field() }}
<input type="text" name="name" id="task-name">
<button type="submit" >Add Task</button>
</form>
</div>
@endsection
Route::get('/', function () {
return view('tasks');
});
Route::post('/task', function (Request $request) {
// validation
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
]);
if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}
// creating the task
$task = new Task;
$task->name = $request->name;
$task->save();
return redirect('/');
});
In resources/views/common/errors.blade.php
@if (count($errors) > 0)
<!-- Form Error List -->
<div class="alert alert-danger">
<strong>Whoops! Something went wrong!</strong>
<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Route::get('/', function () {
$tasks = Task::orderBy('created_at', 'asc')->get();
return view('tasks', [
'tasks' => $tasks
]);
});
tasks.blade.php
@extends('layouts.app') @section('content') // Create Task Form // Current Tasks @if (count($tasks) > 0) <div class="panel panel-default"> <div class="panel-heading"> Current Tasks </div> <div class="panel-body"> <table class="table table-striped task-table"> // Table Headings <thead> <th>Task</th> <th> </th> </thead> // Table Body <tbody> @foreach ($tasks as $task) <tr> // Task Name <td class="table-text"> <div>{{ $task->name }}</div> </td> <td> <form action="/task/{{ $task->id }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }} <button>Delete Task</button> </form> </td> </tr> @endforeach </tbody> </table> </div> </div> @endif @endsection
method_field('DELETE')
will generate to something like this:<input type="hidden" name="_method" value="DELETE">
In route:
Route::delete('/task/{id}', function ($id) {
Task::findOrFail($id)->delete();
return redirect('/');
});
1. Normal PHP
Comment in PHP:
I. Array
Reference: https://www.php.net/manual/en/language.types.array.php
Indexed arrays
Associative arrays
Or
Two-dimensional Arrays
Sorting
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
Operators are used to perform operations on variables and values. PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
For loop
There are 2 types of foreach.