「Php/codeigniter/library」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→LibraryのModelをCI抜きで使う) |
(→constructで文字引数を渡したいとき) |
||
(同じ利用者による、間の6版が非表示) | |||
行30: | 行30: | ||
libraryの引数'mylibrary'は、小文字開始になる | libraryの引数'mylibrary'は、小文字開始になる | ||
− | === | + | ===contructで配列引数を渡す場合=== |
<pre> | <pre> | ||
$params = array('param1' => 'value1', 'param2' => 'value2'); | $params = array('param1' => 'value1', 'param2' => 'value2'); | ||
行40: | 行40: | ||
{ | { | ||
// $paramsを処理 | // $paramsを処理 | ||
+ | } | ||
+ | </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> | </pre> | ||
行82: | 行122: | ||
</pre> | </pre> | ||
コードが追いづらくなるので、あまりおすすめはしない。 | コードが追いづらくなるので、あまりおすすめはしない。 | ||
− | == | + | ==Libraryで、Modelをロードした後、CI抜きで実行する== |
<pre> | <pre> | ||
defined('BASEPATH') OR exit('No direct script access allowed'); | defined('BASEPATH') OR exit('No direct script access allowed'); | ||
行103: | 行143: | ||
</pre> | </pre> | ||
$this->Hello_model->get_data();のように使える | $this->Hello_model->get_data();のように使える | ||
+ | |||
+ | @propertyは、vscodeで、タグジャンプを使う場合の記述 | ||
+ | |||
+ | [[VisualStudioCode/設定/拡張機能/PHP Intelephense]] [ショートカット] |
2025年5月22日 (木) 11: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で、タグジャンプを使う場合の記述