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?

4
  • Are you asking how to create a plain function in PHP? Commented Nov 12, 2011 at 15:33
  • In my example, I want to call TestClass() not by doing a $help->TestClass(). I just wanted it to be just TestClass(); Commented Nov 12, 2011 at 15:35
  • 1
    I think you mean static functions? Commented Nov 12, 2011 at 15:36
  • maybe that is what i'm referring to, not calling a function within a class. Commented Nov 12, 2011 at 15:37

6 Answers 6

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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 :)
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.
1

A standalone function is defined like this:

function myfunction() {
    # whatever
}

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

Comments

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

Comments

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 */
    }
}

Comments

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')); 

Comments

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();

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.