Call function from another function

I have two function

public function test()

{

$characters = 10;

$telephone = ereg_replace("[^0-9]", "", $telephone);

if(strlen($telephone)!=$characters)

{

   list($start, $end) = explode('-', $this->telephone); 


for ($i = $start; $i <= $end; $i++) 


{


      $_this->telephone = $i;


    }

} else {

 	$this->telephone = $this->telephone;

}

}

public function test2()

{

how to call function test1 here

}

HOW TO CALL FUNCTION test1 in test2 and print function test1?

The first function you posted doesn’t return anything.

Actually I don’t know what you are trying to do. In the first function you set $this->telephone, so in second function you probably want to call $this->telephone again?

// But for the actual question:




public function test2()

{

   echo $this->test();

} 



This really depends on where these two functions are located.

If you have just two functions in a file (a php file) then this is the standard:




function test()

{

     some code

}


function test2()

{

     some code

     // run test function

     test();

}



It’s that simple, now the complication is when you have them situated in classes (which is most of any framework)

Then you reference the functions by a $this-> call method. Example:




class example

{


     // same thing applies with variables defined in the class

     private $sample;


     private function test()

   {

          $this->sample = 'Hello';

          echo $this->sample;

   }


    private function test2()

    {

          some code

          // run test function

          $this->test();

   }

}



Now it gets one step further in complication and this is where you’ll see it’s applied all over the framework. What if you want to call those functions within a function in another class?

Well then you have to instantiate that class? That’s why you see


 $model= new Posts();

a lot where Posts is the name of the model. So you can then alter the attributes (variables) in the class by $model->attribute or run the functions by $model->function().

Now if you have to call a function of a class that’s only initiated once or is already running (true for lots of applications in Yii) then you have this format:


ClassName::ClassFunction();

or if the class instantiates another class


ClassName::NewClass()->NewClassFunction();

Hope that helps, if you’re new I highly suggest reading some OOP basics to learn the framework because it’s heavy in OOP structure.

Uhm, sorry guys. This is really related to Yii 1.1.x ? :rolleyes:

Yes.

These are basic PHP questions, not yii specific. So maybe our general PHP section is more apropriate:

http://www.yiiframework.com/forum/index.php?/forum/30-general-php-topics/