• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:PHP 5.2.5で class内のfunctionから)

PHP 5.2.5のclass内functionからの$this->shortの使用方法とエラーについて

このQ&Aのポイント
  • PHP 5.2.5のclass内functionから$this->shortを使用するためにはどのように書けばよいのか?
  • $shortにfunction shortcut()から「ららら」を入れるにはどのように書けばよいのか?
  • PHP 5.2.5のclass内functionから$this->shortを使用すると「Fatal error: Using $this when not in object context」エラーが出る。

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

function shortcut(){ self::$short = "ららら"; } こうじゃないですかねー。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • 関数内関数から関数外関数を呼び出すには?

    abstract class opAshiatoPluginAshiatoActions extends sfActions { public function test() { echo 'test'; } public function executeList($request) {  public function baba()   {   $this->test();   } baba(); } } 成功すれば test と表示されるはずなのですが、エラーログに PHP Fatal error: Using $this when not in object context と出力され正常に表示されません。 関数内関数から関数外関数を呼び出すには$thisでは無くどうすればよいのでしょうか? ご教示頂けると幸いです。 宜しくお願い致します。

    • ベストアンサー
    • PHP
  • php4用のモジュールをphp5環境で動かすには?

    php4用のモジュールなんですけど、php5環境で動かそうと思うと 下記のエラーが出ます。 Fatal error: Using $this when not in object context   ↓の部分でエラーが出ています。 $this->build_params['text_encoding'] = '7bit'; $this->build_params['html_charset'] = 'iso-2022-jp'; $this->build_params['text_charset'] = 'iso-2022-jp'; ソースを貼ってみましたが、どこを変えればよいのでしょうか。。。 http://blog.goo.ne.jp/michimichi_2/e/67b515da0c462aa206dea3de3057f1f0 なにとぞ、よろしくお願いいたします。

    • ベストアンサー
    • PHP
  • classの使い方について

    とある書籍にて require_once "MDB2.php"; require_once "Smarty.class.php"; class BBS extends Smarty{  private $_db;  public function __construct(){   $this->Smarty();   $this->template_dir = "{$dir}/themes";   $this->compile_dir = "{$dir}/templates_c";   $this->config_dir = "{$dir}/config";   $this->config_load("app.conf",basename($_SERVER["SCRIPT_NAME"],".php"));   $this->security=TRUE;   $this->secure_dir=array("{$dir}/image");   $this->_db=MDB2::connect($this->get_config_vars("db_string"));  }  public function __destruct(){   if(!PEAR::isError($this->_db)) $this->_db->disconnect();  }  public function get_db(){ return $this->_db; } } $Bbs = new BBS(); $db = $Bbs->get_db(); $db->setCharset('ujis'); と、あるのですがサーバーがPHP4の為、それに適用させたいのですがどのようにすればいいのでしょうか。ローカルサーバーはPHP5なので問題ないのですが。 class BBS extends Smarty{  var $_db;  function BBS(){   $this->Smarty();   $this->template_dir = "{$dir}/themes";    ~省略~  }  function shutdown(){   if(!PEAR::isError($this->_db)) $this->_db->disconnect();  }  register_shutdown_function("shutdown");  function get_db(){ return $this->_db; } } 上記で試してみたのですが Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in D:\aki\public_html\1\class\smarty.php on line 33 とエラーが出てしまいデストラクタがうまくできない状態です。 PHP4のクラスについて調べてみたが応用できそうなのが見つからなくて・・・ ご教授を願えたらと思います。

    • 締切済み
    • PHP
  • PHP T_STRINGエラー?

    新しくPHPのサイトを作成しようとしたんですが、 syntax error, unexpected '{', expecting T_STRING in と表示されてどうにも使用がありません。 どこが原因でエラーが出てるのでしょうか? エラー行は7行と表示されていますが、いまいちわかりません スクリプトは以下の記述の通りです <?php require './Request.php'; require './Cookie.php'; final class SystemMain extends { private static $instance; private $modeName; public static function getInstance() { if (self::$instance === null) { self::$instance = new SytemMain(); } return self::$instance; } public function setMode() { if (file_exists('./maintenance')) { $this->modeName = 'Maintenance'; } else { $str = Request::both('mode'); if (!$str) { $this->modeName = 'Top'; } else if (file_exists('./script/mode/' . $str . '.php')) { $this->modeName = $str; } else { $this->modeName = 'NotFound'; } } require_once './script/mode/' . $this->modeName . '.php'; } public function getMode() { return $this->modeName; } } ?> Request.php <? class Request extends { public static function get($str) { return isset($_GET[$str]) ? $_GET[$str] : null; } public static function post($str) { return isset($_POST[$str]) ? $_POST[$str] : null; } public static function both($str) { $post = self::post($str); return $post ? $post : self::get($str); } } ?> Cookie.php <? class Cookie extends { public static function set($name, $value, $expire = 0) { return setcookie($name, $value, $expire); } public static function get($str) { return isset($_COOKIE[$str]) ? $_COOKIE[$str] : null; } public static function clear($str) { $_COOKIE[$str] = ""; return setcookie($str, ""); } } ?>

    • ベストアンサー
    • PHP
  • php クラス

    クラスの勉強をしているのですが、 記憶寿命をfunctionの中で完結させsample2以降でも同じ変数などを使えるようにするため定義済み関数内でクラスを処理したいのですが下記のエラーが出てしまいます。 functionの中でクラスを簡潔させるにはどのようにすればよいでしょうか?また、これはなぜエラーが起きているのでしょうか? function sample1(){ $pr=new parson; $pr->name="鈴木"; $pr->age=56; print $pr->getname(); print $pr->getage(); class parson { public $name="鈴木"; public $age=56; function getname(){return $this->name;} function getage(){return $this->age;} }} sample1(); Fatal error: Uncaught Error: Class 'parson' not found in hoge/class.php:11 Stack trace: #0 /hoge/class.php(27): sample1() #1 {main} thrown in /hoge/class.php on line 11

    • ベストアンサー
    • PHP
  • PHP5 toStringについて

    現在PHPの勉強をしております。 XAMPPを利用しローカルで書いたコードをレンタルサーバにアップロードしたところ Object of class hoge could not be converted to string in~ というエラーが発生してしまいます。 PHPのバージョンは XAMPP:PHP5.2.5 レンタルサーバ(xrea):PHP5.2.5 以下エラー部分のコードです。 function MenuSet(){ include_once './class/dataval.class.php'; //DataValは上でincludeしている。ここで上記エラーが発生。 $this->load = new DataVal(); if($_SESSION['account']=='1'){ $other = "<li><a href=\"test.php?$this->load->Count()\">リンク2</a></li>"; } $menu = <<<EOF <ol> <li><a href=\"#\">リンク1</a></li> $other </ol> EOF; echo $menu; } //dataval.class.phpのメソッド function Count(){ $this->filearray = file($_SESSION['userfile']); $this->count = count($this->filearray); return $this->count; } 自分なりに調べた限りでは、オブジェクトを文字列で返す場合は __toStringメソッドを定義しておく、という認識なんですが そもそもどこに定義すれば良いのでしょうか? 同一バージョンでローカルとレンタルサーバで動作が違うところも理解しがたいです… 自分で解決できず悔しいのですが、どなたかヒントをいただけないでしょうか?

    • 締切済み
    • PHP
  • classでエラー

    IIS5+PHP4.4.0+MSDEで、開発しているのですが、 テスト環境では、動いていたソースを、本番環境に動かしたら、 Notice: Undefined variable: m_strDBName in C:\xxx.php on line 110 Fatal error: Call to undefined function: mssql_pconnect() in C:\xxx.php on line 119 というエラーが出てしまいました。 もちろん、テスト環境と、本番環境は、同じ環境で、 PHP.iniもコピーしました。(インストールパスも同じです) エラー部分は、 require_once("xxx.php"); で、呼び出し、 class clsXXX { var $m_strDBName; function clsXXX() { $this->$m_strDBName = "mmdb"; return; } } としています。 どうすれば、解決できるでしょうか?

    • ベストアンサー
    • PHP
  • class内部でarray_mapをつかうとき、なぜこのような記述をしなければならないのでしょうか?

    PHP Version 5.1.6 です。 以下のようなクラスを書いていたのですが、 //----------------------------------------------------------- class PreventAttack{         public static function TopStaticMethod(){             self::ExcludeNullbyte();         }                  private static function ExcludeNullbyte(){             $_GET = self::ExcludeNullbyte_Exec( $_GET );             $_POST = self::ExcludeNullbyte_Exec( $_POST );             $_COOKIE = self::ExcludeNullbyte_Exec( $_COOKIE );             return;                  }             private static function ExcludeNullbyte_Exec($param){              if ( is_array( $param ) ) {              return array_map('ExcludeNullbyte_Exec', $param );              }              return str_replace( "\0", "", $param );             } } //----------------------------------------------------------- Warning: array_map() [function.array-map]: The first argument, 'ExcludeNullbyte_Exec', should be either NULL or a valid callback in /var/www/(中略)include/class/PreventAttack.class on line 39 と怒られてしまいました。 39行目は、下から5行目のarray_mapのところです。 で、このエラーメッセージでグーグルで検索して、いろいろたどっていったら、 http://jp.php.net/manual/ja/language.pseudo-types.php#language.types.callback にたどりつき、 このページのユーザコメント欄に、 //----------------------------------------------------------- To recap mr dot lilov at gmail dot com's comment: If you want to pass a function as an argument to another function, for example "array_map", do this: regular functions: <? array_map(intval, $array) ?> static functions in a class: <? array_map(array('MyClass', 'MyFunction'), $array) ?> functions from an object: <? array_map(array($this, 'MyFunction'), $array) ?> I hope this clarifies things a little bit //----------------------------------------------------------- と書いてあったので、 return array_map(array('ExcludeNullbyte_Exec', $param ); のところを、 return array_map(array('PreventAttack','ExcludeNullbyte_Exec'), $param ); に変えたら、エラーがなくなりました。 ・・・が、その理屈がいまいちわかりません。 どなたかおわかりの方がいらっしゃいましたら、お教えいただけないでしょうか? よろしくお願いいたします。

    • ベストアンサー
    • PHP
  • classのなかのfunctionの中のそのまたfuntionの中で$thisを参照するには?

    classのなかのfunctionの中のそのまたfuntionの中で$thisを参照するには? どうするのでしょうか? 他のObject言語でできることができないので???状態です class Sample { var $a = "a"; function func1() { function func1_1() { echo "func1_1[".$this->a."]"; } echo "func1[".$this->a."]"; func1_1() ; } } $c = new Sample; $c->func1(); -- 出力結果 func1[a]func1_1[] ほしい結果 func1[a]func1_1[a] よろしくお願いします

    • ベストアンサー
    • PHP
  • PHPでの継承について

    PHPの継承についての質問です。 PHPではクラスを継承する場合 スーパークラスのpublicとprotectedなメンバを継承すると いいます。 継承とは、スーパークラスで定義したメンバが サブクラスで定義されたことと同義である。 という前提で質問します。 ひょっとしたらこの前提も微妙に間違っているかもしれません。 以下の用なコードがあるとします。 <?php class AClass { //サブクラスへ継承されるスーパークラスのメソッド public function superClassPublicMethod(){ //【1】 $this-> subClassPrivateMethod(); } } class BClass extends AClass{ public function subClassPublicMethod(){//【2】 $this->subClassPrivateMethod(); } //継承したスーパークラスで定義されているメソッドからコールするサブクラスで定義したメソッド private function subClassPrivateMethod(){ //【3】 print __CLASS__; } } $obj = new BClass(); //サブクラスで定義した public なメソッドを実行 $obj->subClassPublicMethod(); //【4】 //スーパークラスから継承したpublic なメソッドを実行 $obj-> superClassPublicMethod();//【5】 ?> コレを実行すると【4】はきちんとBClassという結果が返ってきます。 ところがその下の【5】を実行すると Fatal error: Call to private method BClass::subClassPrivateMethod() from context 'AClass' in /usr/local/apache/htdocs/ext06.php on line 9 という実行時エラーが返ってきます。これを読むと、AClassのコンテキスト内ではBClassのprivateなメンバは コールできませんよ。というエラーですね。そもそもAClassのコンテキストということは、これはAClass内で実行されているということですよね? ここで、スーパークラスからこのpublicなメソッドは継承しているはずなのになぜ、サブクラスのBCLass内ではなくスーパークラスのAClass内という エラーが返っているのでしょうか? 継承とは、サブクラスで定義したと同義ではなく、スーパークラスまで内部的にアップキャスト?してコールしにいっているのでしょうか? 詳しい方ご教授ください。 よろしく御願いします。

    • ベストアンサー
    • PHP
FMVWC2A37Lのスピーカー音割れ
このQ&Aのポイント
  • FMVWC2A37Lのスピーカー音が割れて聞こえるようになった
  • 原因と元通りにする方法について御教示をお願いします
  • 富士通FMVのスピーカーの音割れについて
回答を見る