Example1>>
<?php
function &_copy(){
// 函數(&)的目的
static $data = array();
return $data;
}
// 呼叫函數還是需要&
$a = &_copy();
$a[] = 5;
// 呼叫函數還是需要&
$b = &_copy();
var_dump($a);
var_dump($b);
------------------------------------------------
Example2>>
function &xyz(Array &$d){
return $d;
}
$data_1 = array(5, 1, 6);
$data_2 = &xyz($data_1);
//----------------------------
var_dump($data_2);
//----------------------------
sort($data_1);
var_dump($data_2);
2018年6月30日 星期六
2018年6月13日 星期三
gseries.js
function gseries(fn, data) {
return new Promise(function (res, rej) {
let it = fn();
job(data);
//----------------------------
function job(d) {
// 主要要不斷重複的步驟
let g = it.next(d);
let p = g.value;
// p.then(...)
if (g.done) {
res(p);
} else if (p instanceof Promise) {
p.then(function (d) {
job(d);
}, function (err) {
rej(err);
});
}else{
job(p);
}
}
});
}
return new Promise(function (res, rej) {
let it = fn();
job(data);
//----------------------------
function job(d) {
// 主要要不斷重複的步驟
let g = it.next(d);
let p = g.value;
// p.then(...)
if (g.done) {
res(p);
} else if (p instanceof Promise) {
p.then(function (d) {
job(d);
}, function (err) {
rej(err);
});
}else{
job(p);
}
}
});
}
java extend 的問題
public class A {
String name = "A";
public void a(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println(name);
}
}
//=====================================
public class B extends A {
String name = "B";
public void b(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println("extend " + name);
}
}
//=====================================
public static void main(String[] args) {
A a = new B();
a.a(); // i am A
a.getName(); // extend B
//----------------------------
B b = (B)a;
b.a(); // 容易出問題 => i am A
b.getName(); // extend B
b.b(); // i am B
}
String name = "A";
public void a(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println(name);
}
}
//=====================================
public class B extends A {
String name = "B";
public void b(){
System.out.println("i am " + name);
}
public void getName(){
System.out.println("extend " + name);
}
}
//=====================================
public static void main(String[] args) {
A a = new B();
a.a(); // i am A
a.getName(); // extend B
//----------------------------
B b = (B)a;
b.a(); // 容易出問題 => i am A
b.getName(); // extend B
b.b(); // i am B
}
2018年6月5日 星期二
讓 js.generator 不斷往下執行
let gseries = require('../node_modules/gseries');
function asynce(index) {
return new Promise(function (res, rej) {
console.log('index(%s) start', index);
setTimeout(function () {
console.log('index(%s) end', index);
res(index);
}, 3000);
});
}
function* main() {
yield asynce(0);
yield asynce(1);
return asynce(2);
}
let p = gseries(main);
--------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
module.exports = gseries;
function gseries(fn, data) {
return new Promise(function (res, rej) {
let it = fn();
job(data);
//----------------------------
function job(d) {
// 主要要不斷重複的步驟
let g = it.next(d);
let p = g.value;
// p.then(...)
if (g.done) {
res(p);
} else if (p instanceof Promise) {
p.then(function (d) {
job(d);
}, function (err) {
rej(err);
});
}else{
job(p);
}
}
});
}
function asynce(index) {
return new Promise(function (res, rej) {
console.log('index(%s) start', index);
setTimeout(function () {
console.log('index(%s) end', index);
res(index);
}, 3000);
});
}
function* main() {
yield asynce(0);
yield asynce(1);
return asynce(2);
}
let p = gseries(main);
--------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
module.exports = gseries;
function gseries(fn, data) {
return new Promise(function (res, rej) {
let it = fn();
job(data);
//----------------------------
function job(d) {
// 主要要不斷重複的步驟
let g = it.next(d);
let p = g.value;
// p.then(...)
if (g.done) {
res(p);
} else if (p instanceof Promise) {
p.then(function (d) {
job(d);
}, function (err) {
rej(err);
});
}else{
job(p);
}
}
});
}
2018年6月2日 星期六
php 檔案鎖定(檔案同步)
PHP flock() 函數
定義和用法
flock() 函數鎖定或釋放文件。若成功,則返回 true。若失敗,則返回 false。
語法
flock(file,lock,block)
| 參數 | 描述 |
|---|---|
| file | 必需。規定要鎖定或釋放的已打開的文件。 |
| lock | 必需。規定要使用哪種鎖定類型。 |
| block | 可選。若設置為 1 或 true,則當進行鎖定時阻擋其他進程。 |
說明
flock() 操作的 file 必須是一個已經打開的文件指針。lock 參數可以是以下值之一:
- 要取得共享鎖定(讀取的程序),將 lock 設為 LOCK_SH(PHP 4.0.1 以前的版本設置為 1)。
- 要取得獨佔鎖定(寫入的程序),將 lock 設為 LOCK_EX(PHP 4.0.1 以前的版本中設置為 2)。
- 要釋放鎖定(無論共享或獨佔),將 lock 設為 LOCK_UN(PHP 4.0.1 以前的版本中設置為 3)。
- 如果不希望 flock() 在鎖定時堵塞,則給 lock 加上 LOCK_NB(PHP 4.0.1 以前的版本中設置為 4)。
提示和註釋
提示:可以通過 fclose() 來釋放鎖定操作,代碼執行完畢時也會自動調用。
註釋:由於 flock() 需要一個文件指針, 因此可能不得不用一個特殊的鎖定文件來保護打算通過寫模式打開的文件的訪問(在 fopen() 函數中加入 "w" 或 "w+")。
-----------------------------------------------------------
例子
a_1.php>>
<?php
ob_start();
$filename = "a1.txt";
$file = fopen($filename, "r+");
$res = '';
// 排它性的锁定
if (flock($file, LOCK_EX)) {
sleep(15);
while(!feof($file)){
$res .= fgets($file);
}
flock($file, LOCK_UN);
} else {
echo "Error locking file!";
}
fclose($file);
printf('<p>finish(%s)</p>', $res);
-----------------------------------------------------------
a_2.php>>
<?php
ob_start();
$filename = "a1.txt";
$file = fopen($filename, "r+");
$res = '';
// 排它性的锁定
if (flock($file, LOCK_EX)) {
while(!feof($file)){
$res .= fgets($file);
}
flock($file, LOCK_UN);
} else {
echo "Error locking file!";
}
fclose($file);
printf('<p>finish(%s)</p>', $res);
訂閱:
文章 (Atom)
