エラーと対策 … 例1

PHP用語集

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


週間人気ページランキング / 9-11 → 9-17
順位 ページタイトル抜粋 アクセス数
1 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 7
1 session_start() | セッション 7
2 「POSIX正規表現」と「PCRE正規表現」の違い 4
2 PHPで定数を定義する方法は2種類ある / 配列定数の定義 4
2 演算子 カテゴリー 4
2 Fatal error: Access level to ▲::$△ must be protected (as in class ●) or weaker | Fatal error(エラーメッセージ) 4
3 定数 3
3 コード例 … 「例外処理」はネストすることができる 3
3 スコープ | 変数 3
3 ob紛らわしい関数()一覧 | 出力バッファリング制御(関数) 3
3 ブラウザを閉じたらセッションデータはどうなるの? | セッション 3
4 Warning: strlen() expects parameter 1 to be string, array given in ○○.php on line △△ | Warning(エラーメッセージ) 2
4 Fatal error: Uncaught Error: Call to a member function modify() on string | Fatal error(エラーメッセージ) 2
4 http_build_query | URLs(関数) 2
4 htmlspecialchars / htmlentities / addslashes / mysql_real_escape_string / mysqli_real_escape_string | セキュリティ 2
4 セッション管理が必要な理由は、HTTPプロトコルには状態を保持する機能がないため | セッション 2
4 Trying to get property of non-object  | Notice(エラーメッセージ) 2
4 エスケープ | セキュリティ 2
4 curl で Cookie を使用する 2
4 Fatal error(エラーメッセージ) カテゴリー 2
2025/9/18 1:01 更新
指定期間人気ページランキング / 2020-5-28 → 2025-9-17
順位 ページタイトル抜粋 アクセス数
1 PHP用語 6697
2 ブラウザを閉じたらセッションデータはどうなるの? | セッション 2521
3 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 2471
4 ブラウザを閉じたらセッションデータはどうなるの? | セッション 1682
5 【テスト投稿】テスト | 1133
6 セッション管理が必要な理由は、HTTPプロトコルには状態を保持する機能がないため | セッション 1068
7 PHPで定数を定義する方法は2種類ある / 配列定数の定義 1016
8 Fatal error: Access level to ▲::$△ must be protected (as in class ●) or weaker | Fatal error(エラーメッセージ) 893
9 Fatal error: Uncaught Error: Call to a member function modify() on string | Fatal error(エラーメッセージ) 854
10 コード例 … 「例外処理」はネストすることができる 848
11 Fatal error: require_once(): Failed opening required 'PEAR.php' | Fatal error(エラーメッセージ) 845
12 curl で Cookie を使用する 840
13 定数 736
14 Fatal error: Uncaught RuntimeException: SplFileObject::__construct(): failed to open stream: Permission denied in | Fatal error(エラーメッセージ) 734
15 インターフェイス | クラスとオブジェクト 697
16 Fatal error: Uncaught HeadlessChromium\Exception\OperationTimedOut: Operation timed out (3sec) in | Fatal error(エラーメッセージ) 685
17 メンバー | クラスとオブジェクト 646
18 Warning: include() [function.include]: Failed opening '**.php' for inclusion (in | Warning(エラーメッセージ) 635
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(エラーメッセージ) 587
2025/9/18 1:01 更新