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

このQ&Aのポイント
  • 関数内関数から関数外関数を呼び出す方法について解説します。
  • 関数内関数から関数外関数を呼び出す際、$thisではなく他の方法を使用する必要があります。
  • 具体的な方法について説明します。
回答を見る
  • ベストアンサー

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

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
  • 回答数1
  • ありがとう数1

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

  • ベストアンサー
  • yambejp
  • ベストアンサー率51% (3827/7415)
回答No.1

$thisを渡してやるとか? <?PHP class hoge{ function fuga(){ echo "fuga"; } function piyo(){ function piyofuga($obj){ $obj->fuga(); } piyofuga($this); } } $hoge = new hoge(); $hoge->piyo(); ?>

people900
質問者

お礼

ありがとうございました!

関連するQ&A

  • PHP 5.2.5で class内のfunctionから

    <?php class other_class{ static $short = 5; function shortcut(){ $this -> short = "ららら"; } }//class print other_class::$short;//ららら になるように ?> PHP 5.2.5で $this -> short を使おうとすると Fatal error: Using $this when not in object context in C:\***\***\***.php on line 7 エラーが出ます。 $short に function shortcut() から "ららら" を入れるにはどう書けばいいでしょうか? よろしくお願いします。m(_ _)m

    • ベストアンサー
    • PHP
  • 継承クラスで定義したメソッドのtry-catch文

    親クラスで、子クラスで実装されたメソッドのExceptionをキャッチしたいのですが、 うまくいきません。 何か良い方法はないでしょうか? abstract class A { public __construct(){ try{ $this->testA(); }catch(Exception $e){ var_dump(1); } } abstract function testA(); } class B extends A { function testA(){ throw Exception('test',1); } } try{ new B; }catch(Exception $e){ var_dump(2); // こっちが動く } よろしくお願いします。

    • ベストアンサー
    • PHP
  • 関数の入れ子ができない。

     はじめまして!php初心者です。 デフォルトではfunctionの入れ子はできないのでしょうか? <? //function test test(); function test(){ echo 'good!<br />'; test1(); function test1 () { echo 'test'; } } ?> これが動きませんでした。 Fatal error*: Call to undefined function test1() in /home/*/public_html/osamu/php/test.php* on line 8 ということでした。入れ子にしたfunctionが駄目らしいです。 <? //function test test(); function test(){ echo 'Omnioo!<br />'; test1(); } function test1 () { echo 'test'; } ?> こんな風に外に出してやる動きます。 機能的に一つの関数にまとめておきたい場合、関数の中に関数がある方が便利だと思うのですが、皆様はどうされているんでしょうか? ご教授お願いします。

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

    zendframeworkを勉強中です。 マニュアルを読んでもプラグインについて理解が深められず質問なのですが 下記のメソッド内でどのような処理を行うべきなのでしょうか? ・routeStartup ・routeShutdown ・dispatchLoopStartup ・preDispatch ・postDispatch ・dispatchLoopShutdown 考えられるのは・・・ DB接続、認証、PCと携帯サイトの切替え、文字コード変換などでしょうか? しかし、文字コードの変換はビューのヘルパーの方が妥当な気もしますが何を どこで処理すべきなのかこんがらかっております・・・ 人によって実装する処理も異なるかと思うのですが一般的な流れなどを教えて いただけますと幸いです。 ちなみに現在は、DBの接続など初期設定を全てBootstrap.phpで行っています。 ※_initDb()など セッションの開始などもBootstrap.phpで行っており、初期設定が増えるに つれて、メソッドも増えるのでソースも長くなってしまいます。 後のメンテを考慮しそれぞれの処理を各プラグイン化しようかと思っている のですがこのような使い方は正しいでしょうか? 最初は、下記のように1つのプラグインを作成しメソッドが実行される タイミングに合わせDBの接続など必要な処理を行い、これで完結すれば いいかと思ったのですが結果、現在のBootstrap.phpと同様です。 class Ext_Plugin_Setup extends Zend_Controller_Plugin_Abstract { public function routeStartup(Zend_Controller_Request_Abstract $request) { echo 'routeStartup<br>'; } public function routeShutdown(Zend_Controller_Request_Abstract $request) { echo 'routeStartup<br>'; } public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) { echo 'routeStartup<br>'; } public function preDispatch(Zend_Controller_Request_Abstract $request) { echo 'routeStartup<br>'; } public function postDispatch(Zend_Controller_Request_Abstract $request) { echo 'routeStartup<br>'; } public function dispatchLoopShutdown() { echo 'routeStartup<br>'; } } 無理にプラグインを使わなくても・・・という事も考えられますが理解を 深めたく思っておりますので詳しい方がいらっしゃいましたらアドバイス お願いいたします。

    • ベストアンサー
    • PHP
  • Object指向で考えた場合、このケースはどうなる

    現在JavaでObject指向を取り入れたプログラミングをしているのですが、疑問が出てきたので質問させていただきます。 例をあげて説明させていただくと、 乗り物、車、ベンツ という3つのObjectがあります。 これらを abstract class 乗り物 abstract class 車 extends 乗りもの class ベンツ extends 車 と書いたのですが、乗り物class内で宣言したabstractメソッドが、ベンツで実装されていなくてもエラーが出ませんでした。 この事から、自分は何か設計を間違えているのではないかと疑問を持ちました。 自分の設計はあっているのでしょうか?ご回答よろしくお願いします。

    • ベストアンサー
    • Java
  • CakePHP redirect関数の文法

    <?php App::uses('AppController', 'Controller'); class DayController extends AppController{ public function index(){ $this->autoRender = false; $date = new DateTime(); $date->setTimeZone(new DateTimeZone('Asia/Tokyo')); $str = $date->format("H:i:s"); $this->redirect("./other/".urlencode($str)); } public function other($param){ $this->autoRender = false; $str = urldecode($param); echo '<html><head><title>現在時刻</title>'; echo '<meta charset="UTF-8"></head>'; echo '<body>'; echo '<h1>現在の時刻は、'.$str. 'です。</h1>'; echo '</body></html>'; } } ?> 上記のようなCakePHPのスクリプトにおいて、下記のようなエラーが返ってきます。 Warning (2): Missing argument 1 for DayController::other() [APP\Controller\DayController.php, Notice (8): Undefined variable: param [APP\Controller\DayController.php, redirect()関数の引数がおかしいのでしょうか。

  • 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
  • CakePHPのredirect()関数について

    <?php App::uses('AppController', 'Controller'); class DayController extends AppController{ public function index(){ $this->autoRender = false; $date = new DateTime(); $date->setTimeZone(new DateTimeZone('Asia/Tokyo')); $str = $date->format("H:i:s"); $this->redirect("./other/".urlencode($str)); } public function other($param){ $this->autoRender = false; $str = urldecode($param); echo '<html><head><title>現在時刻</title>'; echo '<meta charset="UTF-8"></head>'; echo '<body>'; echo '<h1>現在の時刻は、'.$str. 'です。</h1>'; echo '</body></html>'; } } ?> 上記のようなスクリプトをApacheのhtdodos./cakephp/App/Controller フォルダに入れて 動作させると、下記のようなエラーが出力されます。 redirect() 関数の引数がおかしいようですが、どこがどうおかしいのか 教えていただけませんか。 Warning (2): Missing argument 1 for DayController::other() [APP\Controller\DayController.php, line 14] Notice (8): Undefined variable: param [APP\Controller\DayController.php, line 16]

    • 締切済み
    • PHP
  • ajaxのレスポンスが取得できない。

    環境【cakePHP2.X, PHP5.5】 cakephpでajaxを試みてますがうまくいきません。 あるサンプルを参考にして ボタンのクリックをトリガに ajaxMethod()を走らせて alertで"tanaka"を表示させたいのですが なぜかOeders/index.ctpのhtmlを 取得し、長々と表示されてしまいます。 意図した結果を取得できるよう アドバイスを戴けたら嬉しいです。 よろしくお願いします。 【OrdersController.php】 class OrdersController extends AppController { function index() { //初期表示処理 } /** * Ajax用関数 */ function ajaxTest() { $this->autoRender = FALSE; if($this->request->is('ajax')) { return $this->data['name']."さん、こんにちは"; //echoでもOK } } } 【add.ctp】 function ajaxMethod() { $.ajax({ url: "Orders/ajaxTest", type: "POST", data: { name : "tanaka" }, dataType: "text", success : function(response){ //通信成功時の処理 alert(response); }, error: function(){ //通信失敗時の処理 alert('通信失敗'); } }); }

  • 関数の引数とグローバル変数について

    javascript初心者です。 どうしても分からないことがあるので質問させて頂きます。 グローバル変数の値を関数で処理して増やしコンソールログに表示していく、 というようなソースがあるとします。(以下) //グローバル変数 var a = 0; var b = 0; var c = 0; //計算する関数 var afunc = function(){   a++;   console.log(a); } var bfunc = function(){   b++;   console.log(b); } var cfunc = function(){   c++;   console.log(c); //onclickなどで呼び出す関数 function test1(){   var aplus = new afunc(); } function test2(){   var aplus = new bfunc(); } function test3(){   var aplus = new cfunc(); } グローバル変数や関数などが3つと数が少ないならこれでもいいかも知れませんが、 これが数十個とかに増えると、ソースの量もかなり多くなり 管理も大変になると思い簡略化させたいと考えました。 そこで以下のように変えてみたのですが、 加算がうまくいきません。 //グローバル変数 var a = 0; var b = 0; var c = 0; //計算する関数 vvar xxfunc = function(xx){   this.xx = xx;   this.show = function() {     this.xx++;     console.log(this.xx);   } } //onclickなどで呼び出す関数 function test1(){   var aplus = new xxfunc(a);   aplus.show(); } function test2(){   var aplus = new xxfunc(b);   aplus.show(); } function test3(){   var aplus = new xxfunc(c);   aplus.show(); } もしかすると、関数の引数にはグローバル変数を指定することができないのでしょうか? 何かうまいやり方はあるでしょうか? プログラミング自体が勉強し始めたばかりなので、 おかしなソースの書き方をしているかもしれませんのが、 ご教授、よろしくお願いいたします。

専門家に質問してみよう