エラーと対策 … 例1

PHP用語集

カテゴリー: エラーメッセージ  閲覧数:1071 配信日:2013-02-11 00:23


エラー例1
・include出来ない場所に、include文を記述している
class A {// クラス定義
include('config.php');
}
$a = new A();// クラスからインスタンス(オブジェクト)を生成



エラー例1の原因
・includeを書く場所が正しくない
・クラス内で、includeを記述できる箇所は、「コンストラクタ内」「メソッド内」だけ


エラー対策例1-A
・コンストラクタ内にinclude文を記述するよう変更
class A {// クラス定義
 public function __construct() { // コンストラクタ
include('config.php');
 }
}
$a = new A();// クラスからインスタンス(オブジェクト)を生成

エラー対策例1-B
・メソッド内にinclude文を記述するよう変更
class A {// クラス定義
 public function b() {
include('config.php');
 }
}
$a = new A();// クラスからインスタンス(オブジェクト)を生成

エラー対策例1-C
・クラス外にinclude文を記述するよう変更
include('config.php');
class A {// クラス定義
}
$a = new A();// クラスからインスタンス(オブジェクト)を生成



その他
・一般的には、汎用性を考慮して、クラス外でincludeすることの方が多い
▼config.php
$country=japan;

▼index.php
include('config.php');

class A {// クラス定義

 private $var; // このクラス専用プロパティ

 public function __construct($var) { // コンストラクタ
     $this->var = $var; // オブジェクトのプロパティをセット
 }
 
 public function getVar() { //メソッド
     return $this->var; //ゲッター
 }

}      

$a = new A($country);// クラスからインスタンス(オブジェクト)を生成

echo $a->getVar(); // クラスプロパティを取得して出力。'japan’

・「define定数読込」だともっと楽(グローバル変数なので)
▼config.php
define('ADMINIP', '123.45.67.890');//サイト管理者のIP

▼index.php
include('config.php');
class A {// クラス定義

 private $var; // このクラス専用プロパティ

 public function __construct() { // コンストラクタ
     $this->var = ADMINIP; // オブジェクトのプロパティをセット
 }
 
 public function getVar() { //メソッド
     return $this->var; // ゲッター
 }

}      

$a = new A();// クラスからインスタンス(オブジェクト)を生成

echo $a->getVar(); // クラスプロパティを取得して出力。'123.45.67.890’


週間人気ページランキング / 12-6 → 12-12
順位 ページタイトル抜粋 アクセス数
1 Fatal error: Uncaught Error: Call to a member function modify() on string | Fatal error(エラーメッセージ) 4
2 例外処理の構文 3
2 ブラウザを閉じたらセッションデータはどうなるの? | セッション 3
3 PHPで定数を定義する方法は2種類ある / 配列定数の定義 2
3 No route found fo | エラーメッセージ 2
3 Parse error: syntax error, unexpected T_INCLUDE_ONCE, expecting T_FUNCTION | Parse error(エラーメッセージ) 2
3 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 2
3 ( ! ) Fatal error: Uncaught PDOException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column | Fatal error(エラーメッセージ) 2
3 ログイン 2
3 バッファリング | 出力バッファリング制御(関数) 2
3 PHP用語 2
3 Fatal error(エラーメッセージ) カテゴリー 2
3 mixed | 型 2
4 Fatal error: Uncaught RuntimeException: SplFileObject::__construct(): failed to open stream: Permission denied in | Fatal error(エラーメッセージ) 1
4 問題発生 / エラー原因 1
4 session.hash_function(セッション) カテゴリー 1
4 phpinfo(PHP オプションと情報) カテゴリー 1
4 Fatal error: Allowed memory size of ★★ bytes exhausted (tried to allocate ○○ bytes) in △△ on line □□ | Fatal error(エラーメッセージ) 1
4 Composer | 依存関係マネージャ 1
4 popen | ファイルシステム (関数) 1
2025/12/13 1:02 更新
指定期間人気ページランキング / 2020-5-28 → 2025-12-12
順位 ページタイトル抜粋 アクセス数
1 PHP用語 6718
2 ブラウザを閉じたらセッションデータはどうなるの? | セッション 2553
3 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 2510
4 ブラウザを閉じたらセッションデータはどうなるの? | セッション 1707
5 【テスト投稿】テスト | 1133
6 セッション管理が必要な理由は、HTTPプロトコルには状態を保持する機能がないため | セッション 1086
7 PHPで定数を定義する方法は2種類ある / 配列定数の定義 1057
8 Fatal error: Access level to ▲::$△ must be protected (as in class ●) or weaker | Fatal error(エラーメッセージ) 908
9 Fatal error: Uncaught Error: Call to a member function modify() on string | Fatal error(エラーメッセージ) 878
10 コード例 … 「例外処理」はネストすることができる 866
11 curl で Cookie を使用する 862
12 Fatal error: require_once(): Failed opening required 'PEAR.php' | Fatal error(エラーメッセージ) 860
13 Fatal error: Uncaught RuntimeException: SplFileObject::__construct(): failed to open stream: Permission denied in | Fatal error(エラーメッセージ) 748
14 定数 739
15 インターフェイス | クラスとオブジェクト 698
16 Fatal error: Uncaught HeadlessChromium\Exception\OperationTimedOut: Operation timed out (3sec) in | Fatal error(エラーメッセージ) 696
17 メンバー | クラスとオブジェクト 648
18 Warning: include() [function.include]: Failed opening '**.php' for inclusion (in | Warning(エラーメッセージ) 641
19 ( ! ) Fatal error: Uncaught PDOException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column | Fatal error(エラーメッセージ) 610
20 Warning: strlen() expects parameter 1 to be string, array given in ○○.php on line △△ | Warning(エラーメッセージ) 607
2025/12/13 1:02 更新