Parse error: syntax error, unexpected T_INCLUDE, expecting T_FUNCTION

エラーメッセージParse error

 状態:-  閲覧数:4,966  投稿日:2013-02-10  更新日:2013-03-05  
エラーメッセージ
Parse error: syntax error, unexpected T_INCLUDE, expecting T_FUNCTION

エラー原因
・構文エラー
・関数を書くべき箇所に、予期しないincludeがある

エラー対策
・正しい構文へ直す

エラーと対策 … 例1

 閲覧数:833 投稿日:2013-02-11 更新日:2013-03-19 
エラー例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’



Parse error: syntax error, unexpected T_UNSET in ファイル名 on line 行番号

Parse error: syntax error, unexpected T_INCLUDE_ONCE, expecting T_FUNCTION

コメント投稿(ログインが必要)



類似度ページランキング
順位 ページタイトル抜粋
1 Parse error: syntax error, unexpected T_INCLUDE, expecting T_FUNCTION 43
2 Parse error: syntax error, unexpected T_INCLUDE_ONCE, expecting T_FUNCTION 42
3 Parse error: syntax error, unexpected T_REQUIRE, expecting T_FUNCTION 41
4 Parse error: syntax error, unexpected T_REQUIRE_ONCE, expecting T_FUNCTION 39
5 Parse error: syntax error, unexpected 'public' (T_PUBLIC) 38
6 Parse error: syntax error, unexpected T_DECLARE, expecting T_STRING in ○○ on lin 36
7 Parse error: syntax error, unexpected T_DOUBLE_ARROW in △△ on line □□ 36
8 Warning: PDO::query(): LOAD DATA LOCAL INFILE forbidden 35
9 Parse error: syntax error, unexpected 'new' (T_NEW) 35
10 Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection refused 34
11 Parse error: syntax error, unexpected T_UNSET in ファイル名 on line 行番号 34
12 unexpected T_VARIABLE 31
13 Parse error: syntax error, unexpected end of file 30
14 Sorry, that page does not exist. 30
15 unexpected T_STRING 29
16 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation 28
17 Fatal error: Uncaught Error: Class 'Mecab_Tagger' not found in /var/www/ 28
18 ( ! ) Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined 28
19 Fatal error: Uncaught Error: Class 'Stripe\Checkout\Session' not found 27
20 ( ! ) Fatal error: Uncaught Abraham\TwitterOAuth\TwitterOAuthException 27
2024/7/27 17:43 更新
週間人気ページランキング / 7-20 → 7-26
順位 ページタイトル抜粋 アクセス数
1 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 6
2 ブラウザを閉じたらセッションデータはどうなるの? | セッション 5
3 コード例 … 「例外処理」はネストすることができる 4
4 Fatal error: Access level to ▲::$△ must be protected (as in class ●) or weaker | Fatal error(エラーメッセージ) 3
4 PHPで定数を定義する方法は2種類ある / 配列定数の定義 3
4 Parse error: syntax error, unexpected 'new' (T_NEW) | Parse error(エラーメッセージ) 3
4 ブラウザを閉じたらセッションデータはどうなるの? | セッション 3
4 curl で Cookie を使用する 3
4 ( ! ) Deprecated: implode(): Passing glue string after array is deprecated. Swap the parameters in ★★★ headless-chromium-php/vendor/wrench/wrench/lib/Wrench/Protocol/Protocol.php | エラーメッセージ 3
4 or(||) | 演算子 3
5 Composer | 依存関係マネージャ 2
5 グローバル変数 | 変数 2
5 new clone違い | 演算子 2
5 php-devel とは? 2
5 session_start() | セッション 2
5 Fatal error: Uncaught Error: Class 'Mecab_Tagger' not found in /var/www/★★.php:☆☆ | Fatal error(エラーメッセージ) 2
5 例外処理 | 制御構造 2
5 ( ! ) Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined | Fatal error(エラーメッセージ) 2
5 関数 2
6 セッション単位 | セッション 1
2024/7/27 1:01 更新