OOP in PHP: Class-function from a variable?

php class variables
php class example
php class constructor
php class
php object
how do you refer to variables and functions in the base classes in php?
this keyword in php
php static function

Is it possible to call functions from class like this:

$class = new class;
$function_name = "do_the_thing";
$req = $class->$function_name();

Something similar solution, this doesn't seem to work?

Yes, it is possible, that is know as variable functions, have a look at this.

Example from PHP's official site:

<?php
class Foo
{
    function Variable()
    {
        $name = 'Bar';
        $this->$name(); // This calls the Bar() method
    }

    function Bar()
    {
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable()

?>

In your case, make sure that the function do_the_thing exists. Also note that you are storing the return value of the function:

$req = $class->$function_name();

Try to see what the variable $req contains. For example this should give you info:

print_r($req); // or simple echo as per return value of your function

Note:

Variable functions won't work with language constructs such as echo(), print(), unset(), isset(), empty(), include(), require() and the like. Utilize wrapper functions to make use of any of these constructs as variable functions.

Classes and Objects - Manual, A class may contain its own constants, variables (called "properties"), and Calling a non-static method statically from an incompatible context has been My absolute favorite thing about OOP is that "good" objects keep themselves in check. We can define variables like $name, $color, and $weight to hold the values of these properties. When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

My easiest example is:

$class = new class;
$function_name = "do_the_thing";
$req = $class->${$function_name}();

${$function_name} is the trick

Also works with static methods:

$req = $class::{$function_name}();

The Basics - Manual, var $object; var $class; var $table; function LFPDataFactory($args) { $this->​unpackArgs($args); // assigns locals from $args if (in_array(strtolower($this->​class),  PHP 5 is very very flexible in accessing member variables and member functions. These access methods maybe look unusual and unnecessary at first glance; but they are very useful sometimes; specially when you work with SimpleXML classes and objects.

You can use ReflectionClass.

Example:

$functionName = 'myMethod';
$myClass = new MyClass();
$reflectionMyMethod = (new ReflectionClass($myClass))->getMethod($functionName);

$relectionMyMethod->invoke($myClass); // same as $myClass->myMethod();

Remember to catch ReflectionException If Method Not Exist.

Classes/Object Functions - Manual, The class definition starts with the keyword class followed by a class name, then followed by a set of curly braces ({}) which enclose constants, variables (called "properties"), and functions (called "methods") belonging to the class. OOP - Classes and objects - Create Class OOP (Object Oriented Programming) is a programming concept (or technique), which treats data and functions as objects. Important to this concept is to understand the difference between a Class and an Object. - A class is a "blueprint" for an object, is a code template used to generate objects.

PHP: Object Oriented Programming, Accessing instance's variable/functions. 1.1 Example: MyCircle Class. We shall begin with a class called MyCircle , which models a circle with a specific radius  Do not confuse php's version of properties with properties in other languages (C++ for example). In php, properties are the same as attributes, simple variables without functionality. They should be called attributes, not properties. Properties have implicit accessor and mutator functionality.

Object-Oriented Programming (OOP) in PHP, This tutorial is a beginners look at OOP(object oriented programming) in PHP and covers what a class, method and variables are as well as  Class Abstraction. PHP 5 introduces abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation.

What are Classes and Methods in OOP PHP, Object Oriented PHP for Beginners: Steps 1 – 5. For this tutorial, you should understand a few PHP basics: functions, variables, conditionals and loops. To make  Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.

Comments
  • possible duplicate of stackoverflow.com/questions/936875/…
  • @MAS1: Yeah it is very important :)
  • It's also good because calling class functions this way is case insensitive! :)
  • I get an error using $req = $class->${$function_name}(); However the following seems to be the correct syntax $req = $class->{$function_name}();
  • thanks @Martin for the fix. I will need to check again with the code where it comes from to see why I had this error.
  • Methods using variable variables, nice!