1

I saw some codes that when they call php functions from another class they no longer use $this->functionName(), they just refer immedietly to the function name, like functionName()

In my index.php

 $help = new Helper();
 $help->Test();

I wanted to call Test Function by not doing the $help.

How can this be done? Why is this possible?

PinoyStackOverflower
  • 5,214
  • 18
  • 63
  • 126

6 Answers6

4

In PHP you can mix a procedural style of programming with object oriented style. That means that function can either exist as member of a class, or as stand-alone functions.

Member functions (or methods) are are called using $classinstance->methodname() for normal (instance) methods, or ClassName::methodName() for static methods.

Standalone functions are just called without referring to a class or object whatsoever. You can put them in separate files, if you like. The declaration and usage is as follows:

In example.php:

class MyClass
{
  $member = 'Hello world';

  function MyMethod()
  {
    // The method can use the instance variable (member variable) 
    // using $this to refer to the instance of the class
    echo $this->member;  
  }

  static function MyStaticMethod()
  {
    echo 'Hello static world';
  }

}

function MyFunction()
{
  echo 'Hello';
}

In index.php:

// To include the class and its methods, as well as the function.
require_once 'example.php';

// Call an instance method
$instance = new MyClass();
$instance->MyMethod();

// Call a static method
MyClass::MyStaticMethod();

// Call a stand-alone function
MyFunction();
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • maybe this is what i'm refering to. Can you show me a code on how to call a standalone function in another php file. Thanks :) – PinoyStackOverflower Nov 12 '11 at 15:38
  • I just added some code. You can call functions in any PHP file, as long as you include that file. Including files with functions is best done using `require_once` or `require`. This goes not just for functions; any php file can be included that way. – GolezTrol Nov 12 '11 at 15:42
1

With the -> operator you reference a function from within a class.

<?php
class A {
  public function a() {
    $this->b();  //references the function b() in $this class
  }

  public function b() {
    echo 'Was called from function a() in class A';
  }
}

function c() {
  echo "I'm just a simple function outside a class";
}

//now you can do following calls
$class_a = new A();
$class_a->a();
c(); //references function c() within the same scope

The output would be:

Was called from function a() in class A

I'm just a simple function outside a class

But you could also do the following: outsource the function c() into an external file like function_c.php

Now, you can include/require the file from anywhere else and use it's content:

include 'function_c.php';
c(); //the function is now available, although it was defined in another file
Community
  • 1
  • 1
Quasdunk
  • 14,944
  • 3
  • 36
  • 45
1

A standalone function is defined like this:

function myfunction() {
    # whatever
}

Also see http://www.php.net/manual/en/functions.user-defined.php

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
0

you can a function from another class from a class, example:

require "myExternClass.php";
class myClass extends myExternClass
{
    public function a() {
            $this->b(); /* function b is in the class myExternClass */
    }
}
noob
  • 8,982
  • 4
  • 37
  • 65
0

You can wrap the code in question inside a regular function:

function TestClass() {
  $help = new Helper();
  return $help->Test();
}

Then, in your index.php file you can call the function like this:

TestClass();
Rob Apodaca
  • 834
  • 4
  • 8
  • People who've got some knowledge on the subject will understand this code, but it doesn't make it clearer to those who haven't. – GolezTrol Nov 12 '11 at 15:43
0

generally you can't call a method of an object without the object itself. but for some cases when method does not actually uses any objects' properties it may be acceptable for testing purposes to invoke it with call_user_func_array, passing some dummy value instead of object.

 class A     {
    var $a;

    function doNop() { echo "Nop";}
    function doA() { echo "[".$a."]"; }
 }

 // instead of this
 $a = new A; 
 $a->doNop();

 // you _may_ use this
 A::doNop();

 // but this will fail, because there's no object to apply doA() to.
 A::doA();

 class A_dummy { $a };

 // however, for testing purposes you can provide a dummy instead of real A instance
 $b = new A_dummy;
 call_user_func(array($b, 'A::doA')); 
shomeax
  • 855
  • 6
  • 12