calling class file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

calling class file

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I cannot reproduce that behaviour in php.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
Post Reply