エラーと対策 … 例1

PHP用語集

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


週間人気ページランキング / 11-28 → 12-4
順位 ページタイトル抜粋 アクセス数
1 ブラウザを閉じたらセッションデータはどうなるの? | セッション 4
2 クロージャ | 関数 3
2 set_error_handler | 例外処理(制御構造) 3
2 「POSIX正規表現」と「PCRE正規表現」の違い 3
2 curl で Cookie を使用する 3
3 言語構造 2
3 Warning: strlen() expects parameter 1 to be string, array given in ○○.php on line △△ | Warning(エラーメッセージ) 2
3 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 2
3 PHPで定数を定義する方法は2種類ある / 配列定数の定義 2
3 Fatal error: Uncaught Error: Call to a member function modify() on string | Fatal error(エラーメッセージ) 2
3 Fatal error: Uncaught League\OAuth1\Client\Credentials\CredentialsException: Received HTTP status code [401] with message "現在この機能は一時的にご利用いただけません" when getting token credentials. | Fatal error(エラーメッセージ) 2
3 define と const の違い | 定数 2
3 MySQL接続方法比較 | データベース関連 2
3 ob紛らわしい関数()一覧 | 出力バッファリング制御(関数) 2
3 エラーレベル | エラー設定(エラーメッセージ) 2
3 対応例1.メモリ不足なので、PHPの最大使用メモリを増加するよう変更 2
3 PHPにおけるメソッドのオーバーライドについて /「引数の数や型は、親クラスのメソッドと完全に一致していなければなりません。」とは具体的にどういう意味ですか? 2
3 Warning: mb_convert_encoding(): Unable to detect character encoding | Warning(エラーメッセージ) 2
3 Parse error: syntax error, unexpected 'new' (T_NEW) | Parse error(エラーメッセージ) 2
3 Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | エラーメッセージ 2
2025/12/5 1:01 更新
指定期間人気ページランキング / 2020-5-28 → 2025-12-4
順位 ページタイトル抜粋 アクセス数
1 PHP用語 6716
2 ブラウザを閉じたらセッションデータはどうなるの? | セッション 2552
3 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 2507
4 ブラウザを閉じたらセッションデータはどうなるの? | セッション 1703
5 【テスト投稿】テスト | 1133
6 セッション管理が必要な理由は、HTTPプロトコルには状態を保持する機能がないため | セッション 1086
7 PHPで定数を定義する方法は2種類ある / 配列定数の定義 1055
8 Fatal error: Access level to ▲::$△ must be protected (as in class ●) or weaker | Fatal error(エラーメッセージ) 907
9 Fatal error: Uncaught Error: Call to a member function modify() on string | Fatal error(エラーメッセージ) 874
10 コード例 … 「例外処理」はネストすることができる 865
11 curl で Cookie を使用する 861
12 Fatal error: require_once(): Failed opening required 'PEAR.php' | Fatal error(エラーメッセージ) 859
13 Fatal error: Uncaught RuntimeException: SplFileObject::__construct(): failed to open stream: Permission denied in | Fatal error(エラーメッセージ) 747
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(エラーメッセージ) 606
2025/12/5 1:01 更新