php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18864 Nested Function within a class
Submitted: 2002-08-12 04:07 UTC Modified: 2002-08-13 05:42 UTC
From: fathireza at yahoo dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.2.2 OS: NT2000
Private report: No CVE-ID: None
 [2002-08-12 04:07 UTC] fathireza at yahoo dot com
Hello,

I have no access to "this variable" within the nested function.

   class   A{
      var someX;
      
      function b()
      {
         function c()
         {
           $this->someX=1;
           //Error this variable unknow here
         }
       }
   }

Best regards...
R. Fathi

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-13 01:03 UTC] kalowsky@php.net
Why would you need this functionality?  Why not just create a second function within the class, and call it there?
 [2002-08-13 03:36 UTC] fathireza at yahoo dot com
"Why would you need this functionality?" does not really 
answer my question/problem.
   Anyhow there is a reason for it and I am developing some
reusable classes for the project I am working on.

Thanks...
Anyhow
 [2002-08-13 03:56 UTC] rasmus@php.net
There is absolutely no difference between:

function b() {
  function c() {
  }
}

and:

function b() { }
function c() { }

in terms of their calling scope.  They are identical.  It's not like function c won't be defined until function b is called in the first case.  So what you are doing is meaningless.
 [2002-08-13 04:02 UTC] fathireza at yahoo dot com
Well dear rasmus what you said is totally meaningless. Try
to access "this" variable in any order of function calling
that you wish, but it is not accessible in any case.
These comments are worthless and does not solve the scoping
problem with php 4.2.2.
Thanks..
R. Fathi
 [2002-08-13 04:06 UTC] rasmus@php.net
What are you talking about?  Run this:

<?
class A {
  var $someX;
      
  function b() {
  }
  function c() {
     $this->someX=1;
  }
}

$foo = new A;
$foo->c();
echo $foo->someX;
?>
 [2002-08-13 05:28 UTC] pguillot at paanjaru dot com
Dear Fathi,

I don't understand clearly what you want to do :
Your variable is 
- recognized only in the function where it is defined
- recognized everywhere if it is __global__

It seems you want to create a class-global var !
(Q: in which real case is it useful ? )
Could'nt you pass the var when calling your last function ?

Anyhow, thanks to all for the dev, support and use cases ... After so much time using Php, i'm still amazed by it's power and speed (imho).

Patrick
 [2002-08-13 05:37 UTC] pguillot at paanjaru dot com
Sight,

I should have read more carefully :
I saw nested classes where functions were present.
btw, I should have said :
Your variable is 
- recognized only in the function where it is defined
- recognized everywhere if it is __global__
- recognized in the class where it's defined

Patrick
 [2002-08-13 05:42 UTC] fathireza at yahoo dot com
Dear Rasmus,
This is what I am basically trying to do. If there is 
anything wrong with this, please let me know.
This is what I get
Notice: Undefined variable: this in c:\inetpub\wwwroot\phpsite\testthis.php on line 17

Thanks..
R. Fathi

<?PHP
   class   A {
      var $someX;
	  
	  function A($str)
	  {
	  	//......
	  	$this->b($str);
      }
      function b($num)
      {
	  	 $func="c_".$num;
		 $this->someX=1;
         function c_1()
         {
           //Error this variable unknow here
		   print "$this->someX";
         }
		 function c_2()
		 {
		 }
		 //....
		 $func();
       }
   }
$foo = new A("1");
?>
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 28 06:03:33 2021 UTC