「Php/codeigniter/library」の版間の差分
ナビゲーションに移動
検索に移動
| (同じ利用者による、間の14版が非表示) | |||
| 16行目: | 16行目: | ||
public function my_method($param = '') | public function my_method($param = '') | ||
{ | { | ||
return 'Result: ' . print_r($param,1); | return 'Result: ' . print_r($param,1); | ||
} | } | ||
| 26行目: | 25行目: | ||
$this->load->library('mylibrary'); | $this->load->library('mylibrary'); | ||
echo $this->mylibrary->my_method('parameter'); | echo $this->mylibrary->my_method('parameter'); | ||
$params = array('param1' => 'value1', 'param2' => 'value2'); | |||
echo $this->mylibrary->my_method($params); | |||
</pre> | </pre> | ||
libraryの引数'mylibrary'は、小文字開始になる | libraryの引数'mylibrary'は、小文字開始になる | ||
=== | ===contructで配列引数を渡す場合=== | ||
<pre> | <pre> | ||
$params = array('param1' => 'value1', 'param2' => 'value2'); | $params = array('param1' => 'value1', 'param2' => 'value2'); | ||
| 41行目: | 42行目: | ||
} | } | ||
</pre> | </pre> | ||
配列意外の文字列とかだとエラーになる。 | |||
===constructのloadで文字引数を渡したとき=== | |||
文字を渡すとエラーになる。 | |||
<pre> | |||
Severity: error --> Exception: Too few arguments to function Mylibrary::__construct(), 0 passed in ~/system/core/Loader.php on line 1286 and exactly 1 expected ~/application/libraries/Mylibrary.php 7 | |||
</pre> | |||
ngコード | |||
<pre> | |||
// 呼び出し側 | |||
$this->load->library('mylibrary', "taro"); | |||
// ライブラリクラス側 | |||
public function __construct($name) | |||
{ | |||
} | |||
</pre> | |||
okコード | |||
<pre> | |||
// 呼び出し側 | |||
$params = array('name' => 'taro'); | |||
$this->load->library('mylibrary', $params); | |||
// ライブラリクラス側 | |||
public function __construct($params = []) | |||
{ | |||
echo $params['name']; | |||
} | |||
</pre> | |||
===constructで文字引数を渡したいとき=== | |||
手動でnewして渡せばよい | |||
<pre> | |||
$this->load->library('mylibrary'); | |||
$mylibrary = new Mylibrary($name); | |||
// ライブラリクラス側 | |||
public function __construct($name) | |||
{ | |||
echo $name; | |||
} | |||
</pre> | |||
==Libraryで、Modelをロードする== | |||
<pre> | |||
class Mylibrary { | |||
public function __construct($params = []) | |||
{ | |||
// コンストラクタ | |||
} | |||
public function hello() | |||
{ | |||
$CI =& get_instance(); | |||
$CI->load->model('Hello_model'); | |||
echo print_r($CI->Hello_model->get_data()); | |||
} | |||
} | |||
</pre> | |||
==コントローラーのプロパティをLibraryで使う== | |||
コントローラーの$this->hogehogeをLibraryで使う場合のサンプル | |||
<pre> | |||
class Library extends MY_Controller { | |||
public function index() | |||
{ | |||
$this->load->library('mylibrary'); | |||
$this->hogehoge = "hogehoge"; | |||
$this->mylibrary->hello(); | |||
} | |||
} | |||
</pre> | |||
ライブラリ | |||
<pre> | |||
class Mylibrary { | |||
public function hello() | |||
{ | |||
$CI =& get_instance(); | |||
echo $CI->hogehoge; | |||
} | |||
} | |||
</pre> | |||
コードが追いづらくなるので、あまりおすすめはしない。 | |||
==Libraryで、Modelをロードした後、CI抜きで実行する== | |||
<pre> | |||
defined('BASEPATH') OR exit('No direct script access allowed'); | |||
/** | |||
* @property Hello_model $Hello_model | |||
*/ | |||
class Mylibrary { | |||
public function __construct() | |||
{ | |||
$CI =& get_instance(); | |||
$CI->load->model('Hello_model'); | |||
$this->Hello_model =& $CI->Hello_model; | |||
} | |||
public function hello() | |||
{ | |||
$this->Hello_model->get_data(); | |||
} | |||
} | |||
</pre> | |||
$this->Hello_model->get_data();のように使える | |||
@propertyは、vscodeで、タグジャンプを使う場合の記述 | |||
[[VisualStudioCode/設定/拡張機能/PHP Intelephense]] [ショートカット] | |||
2025年5月22日 (木) 02:33時点における最新版
ライブラリの使い方
application/libraries/ディレクトリに新しいライブラリファイルを作成する
application/libraries/Mylibrary.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mylibrary {
public function __construct()
{
// コンストラクタ
}
public function my_method($param = '')
{
return 'Result: ' . print_r($param,1);
}
}
ライブラリの呼び出し方
$this->load->library('mylibrary');
echo $this->mylibrary->my_method('parameter');
$params = array('param1' => 'value1', 'param2' => 'value2');
echo $this->mylibrary->my_method($params);
libraryの引数'mylibrary'は、小文字開始になる
contructで配列引数を渡す場合
$params = array('param1' => 'value1', 'param2' => 'value2');
$this->load->library('mylibrary', $params);
ライブラリクラス側
public function __construct($params = [])
{
// $paramsを処理
}
配列意外の文字列とかだとエラーになる。
constructのloadで文字引数を渡したとき
文字を渡すとエラーになる。
Severity: error --> Exception: Too few arguments to function Mylibrary::__construct(), 0 passed in ~/system/core/Loader.php on line 1286 and exactly 1 expected ~/application/libraries/Mylibrary.php 7
ngコード
// 呼び出し側
$this->load->library('mylibrary', "taro");
// ライブラリクラス側
public function __construct($name)
{
}
okコード
// 呼び出し側
$params = array('name' => 'taro');
$this->load->library('mylibrary', $params);
// ライブラリクラス側
public function __construct($params = [])
{
echo $params['name'];
}
constructで文字引数を渡したいとき
手動でnewして渡せばよい
$this->load->library('mylibrary');
$mylibrary = new Mylibrary($name);
// ライブラリクラス側
public function __construct($name)
{
echo $name;
}
Libraryで、Modelをロードする
class Mylibrary {
public function __construct($params = [])
{
// コンストラクタ
}
public function hello()
{
$CI =& get_instance();
$CI->load->model('Hello_model');
echo print_r($CI->Hello_model->get_data());
}
}
コントローラーのプロパティをLibraryで使う
コントローラーの$this->hogehogeをLibraryで使う場合のサンプル
class Library extends MY_Controller {
public function index()
{
$this->load->library('mylibrary');
$this->hogehoge = "hogehoge";
$this->mylibrary->hello();
}
}
ライブラリ
class Mylibrary {
public function hello()
{
$CI =& get_instance();
echo $CI->hogehoge;
}
}
コードが追いづらくなるので、あまりおすすめはしない。
Libraryで、Modelをロードした後、CI抜きで実行する
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* @property Hello_model $Hello_model
*/
class Mylibrary {
public function __construct()
{
$CI =& get_instance();
$CI->load->model('Hello_model');
$this->Hello_model =& $CI->Hello_model;
}
public function hello()
{
$this->Hello_model->get_data();
}
}
$this->Hello_model->get_data();のように使える
@propertyは、vscodeで、タグジャンプを使う場合の記述