エラーと対策 … 例1

PHP用語集

カテゴリー: エラーメッセージ  閲覧数:1015 配信日: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’


週間人気ページランキング / 8-21 → 8-27
順位 ページタイトル抜粋 アクセス数
1 PHPで定数を定義する方法は2種類ある / 配列定数の定義 6
2 ブラウザを閉じたらセッションデータはどうなるの? | セッション 4
2 対応例1.メモリ不足なので、PHPの最大使用メモリを増加するよう変更 4
3 http_build_queryとは? / 構文 /パラメータ 3
3 Fatal error: Access level to ▲::$△ must be protected (as in class ●) or weaker | Fatal error(エラーメッセージ) 3
3 定数 3
4 session.hash_function | セッション 2
4 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 2
4 「include_once」で「An error occurred.」と表示される場合の原因は、大きく分けると 2 種類あります。 | エラーメッセージ 2
4 htmlspecialchars / htmlentities / addslashes / mysql_real_escape_string / mysqli_real_escape_string | セキュリティ 2
4 Fatal error: require_once(): Failed opening required 'PEAR.php' | Fatal error(エラーメッセージ) 2
4 dirname() / (__FILE__ ) / basename( dirname( __FILE__ ) ); | 関数 2
4 PHPにおけるメソッドのオーバーライドについて /「引数の数や型は、親クラスのメソッドと完全に一致していなければなりません。」とは具体的にどういう意味ですか? 2
5 spliceImageメソッド | Imagickクラス 1
5 エラー | 例外処理(制御構造) 1
5 Parse error: syntax error, unexpected T_REQUIRE_ONCE, expecting T_FUNCTION | Parse error(エラーメッセージ) 1
5 リファレンスを返す | リファレンス 1
5 セッション・ハイジャック | セキュリティ 1
5 エラーメッセージ / エラー原因 / エラー対応 1
5 Composer | 依存関係マネージャ 1
2025/8/28 1:01 更新
指定期間人気ページランキング / 2020-5-28 → 2025-8-27
順位 ページタイトル抜粋 アクセス数
1 PHP用語 6696
2 ブラウザを閉じたらセッションデータはどうなるの? | セッション 2513
3 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 2458
4 ブラウザを閉じたらセッションデータはどうなるの? | セッション 1670
5 【テスト投稿】テスト | 1133
6 セッション管理が必要な理由は、HTTPプロトコルには状態を保持する機能がないため | セッション 1062
7 PHPで定数を定義する方法は2種類ある / 配列定数の定義 1004
8 Fatal error: Access level to ▲::$△ must be protected (as in class ●) or weaker | Fatal error(エラーメッセージ) 888
9 Fatal error: Uncaught Error: Call to a member function modify() on string | Fatal error(エラーメッセージ) 849
10 コード例 … 「例外処理」はネストすることができる 841
11 Fatal error: require_once(): Failed opening required 'PEAR.php' | Fatal error(エラーメッセージ) 839
12 curl で Cookie を使用する 837
13 Fatal error: Uncaught RuntimeException: SplFileObject::__construct(): failed to open stream: Permission denied in | Fatal error(エラーメッセージ) 732
14 定数 729
15 インターフェイス | クラスとオブジェクト 697
16 Fatal error: Uncaught HeadlessChromium\Exception\OperationTimedOut: Operation timed out (3sec) in | Fatal error(エラーメッセージ) 684
17 メンバー | クラスとオブジェクト 645
18 Warning: include() [function.include]: Failed opening '**.php' for inclusion (in | Warning(エラーメッセージ) 632
19 ( ! ) Fatal error: Uncaught PDOException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column | Fatal error(エラーメッセージ) 602
20 Warning: strlen() expects parameter 1 to be string, array given in ○○.php on line △△ | Warning(エラーメッセージ) 582
2025/8/28 1:01 更新