Call non static methods from static methods

I

 

 

t is very good question for interview context and it is good oops concept.

First let me explain about static method –

Static method is types of methods in which , this method is called by the class name not the object . This is very important . $this is not available in static method . Only allowed static ,self and parents are allowed.

Example —

Class abc {

Static function test()

{

return ‘hii’;

}

}

abc::test()

 

Output — hi

Note — if you call this method from object of this class . It will be throw error..

Example —

Class abc {

Static function test()

{

return ‘hi’;

}

}

$obj = new abc ()

$obj->test()

Output — error will be throw ..

 

But when we talk about non static method . It is called by the object of the class and $ this is available in this function.

 

Example —

Class abc {

Function test ()

{

Return ‘hi’;

}

}

$obj = new abc ()

$obj->test();

Output — hi

 

Call non static method from static method —

 

Class abc {

Static function test()

{

Obj = new abc()

Obj->nonstatic();

}

Function nonstatic(){

Return ‘hii’

}

}

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *