「Php/codeigniter/library」の版間の差分
ナビゲーションに移動
検索に移動
| 30行目: | 30行目: | ||
libraryの引数'mylibrary'は、小文字開始になる | libraryの引数'mylibrary'は、小文字開始になる | ||
=== | ===contructで配列引数を渡す場合=== | ||
<pre> | <pre> | ||
$params = array('param1' => 'value1', 'param2' => 'value2'); | $params = array('param1' => 'value1', 'param2' => 'value2'); | ||
| 44行目: | 44行目: | ||
配列意外の文字列とかだとエラーになる。 | 配列意外の文字列とかだとエラーになる。 | ||
===constructで文字引数を渡したとき=== | |||
文字を渡すとエラーになる。 | |||
<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"); | |||
</pre> | |||
ライブラリクラス側 | |||
<pre> | |||
public function __construct($name) | |||
{ | |||
} | |||
</pre> | |||
okコード | |||
<pre> | |||
$params = array('name' => 'taro'); | |||
$this->load->library('mylibrary', $params); | |||
</pre> | |||
ライブラリクラス側 | |||
<pre> | |||
public function __construct($params = []) | |||
{ | |||
echo $params['name']; | |||
} | |||
</pre> | |||
==Libraryで、Modelをロードする== | ==Libraryで、Modelをロードする== | ||
2025年5月22日 (木) 01:53時点における版
ライブラリの使い方
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で文字引数を渡したとき
文字を渡すとエラーになる。
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'];
}
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で、タグジャンプを使う場合の記述