Home  >  Article  >  Backend Development  >  PHP implements the object usage of dynamically created classes with variables

PHP implements the object usage of dynamically created classes with variables

墨辰丷
墨辰丷Original
2018-05-26 13:59:302796browse

This article mainly introduces the use of variables in PHP to dynamically create class objects, involving operating techniques related to the dynamic creation of objects in PHP object-oriented programming. Friends in need can refer to the following

The examples in this article describe PHP Use variables to dynamically create objects of a class. Share it with everyone for your reference, the details are as follows:

This is a usage that can use variables to dynamically create objects of classes, which is to create objects based on the value of the $pay_code variable.

For example, the following example is Create an object $payment of class T. Then you can use it. At first glance, it seems like a new knowledge point, but it is not. It is just a skill in programming.

$pay_code='T';
$payment = new $pay_code();
echo $payment;
echo $payment->ep();
class T{
  function ep(){
    return '我是T里面的方法!';
  }
  function __toString(){
    return '这是个T类';
  }
}
class W{
  function ep(){
    return '我是W里面的方法!';
  }
  function __toString(){
    return '这是个W类';
  }
}

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP variablesDetailed explanation and string dynamic insertion of variables (case)

Memory Management of PHP Variables

##PHP VariablesScope Detailed Explanation

The above is the detailed content of PHP implements the object usage of dynamically created classes with variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn