Page 1 of 1

calling class file

Posted: Wed Aug 08, 2007 7:25 am
by shivam0101
I have a main class in which i am calling different class files by using :: (scope resolution operator). In the other classes i.e the class files i am using $this-> operator to call the methods of main class as well as methods of other classes, i am able to access all their methods. (without using extend).

For example
main class

Code: Select all

require_once('classA.php');
require_once('classB.php');
require_once('classC.php');

Class Main {

// methods of main class


  function EchoError($error)
  {
    return "<b>$error</b>";
  } 
.
.


// methods of other classes called, so that i can call all the methods of any class using one class obj.  I am using like a connector to other classes

  


  function callAmethodFromClassA(param1)
  {
    return ClassA :: callAmethodFromClassA(param1);
  }

  function callAmethodFromClassB(param1)
  {
    return ClassB :: callAmethodFromClassB(param1);
  }
   
// so on

}


In ClassA file

require_once('mainclass.php');
require_once('classB.php');
require_once('classC.php');

Class ClassA
{
   function FirstMethodOfClassA()
  {
     $this->EchoError('this is error1');
  }

  function SecondMethodOfClassA()
  {
     $this->callAmethodFromClassB(param1);

  }
}
in the same way classB and other class are written. I am not extending classes still i am able to access using $this operator

Posted: Wed Aug 08, 2007 8:15 am
by volka
I cannot reproduce that behaviour in php.

Posted: Thu Aug 09, 2007 1:10 am
by shivam0101
I cannot reproduce that behaviour in php.
can you please check this,

Code: Select all

require_once('classA.php');
class Main
{

    function CheckWhereIam($name)
   {
       return ClassA :: CheckWhereIam()
   }

   function CheckWhereIamMain()
   {
         echo 'I am in main class';
   }

}


require_once('classMain.php');
class ClassA 
{
   function CheckWhereIam()
   {
       $this->CheckWhereIamMain();
    
   }

}
calling file

Code: Select all

require_once('classMain.php');
require_once('classA.php');


$obj=new Main();


$obj->CheckWhereIam();
As you can see, without using extends, i am able to access the CheckWhereIamMain(); using $this operator without extending

Posted: Thu Aug 09, 2007 3:18 am
by stereofrog
This behavior is deprecated and shouldn't be relied on. You might want to add

Code: Select all

error_reporting(E_ALL | E_STRICT);"
to your main script.

Posted: Thu Aug 09, 2007 4:42 am
by shivam0101
Thanks for that, stereofrog

I am getting may errors displayed. But still i could not understand that how i could access methods with $this operator without extending the class.

Posted: Thu Aug 09, 2007 6:19 am
by stereofrog
Here's your example in a more concise form

Code: Select all

class A 
{
	var $prop_of_a = "HELLO";
	
	function foo_a() {
		B :: foo_b();
	}
}

class B 
{
	function foo_b() {
		echo $this->prop_of_a;
	}
}

$a = new A;
$a->foo_a(); // HELLO
You're wondering why B has access to the property of A via $this, although B doesn't extend A. The answer is, that php4 used to propagate "$this" across static calls, i.e. if in context of some method you're calling some other method statically ( :: ), the current value of $this is passed to that second method. This behavior is deprecated but still supported in php5. Note that php6 will completely disallow static calls to non-static methods and vice versa.