Python has a function where we can execute a other python file and get methods from that file in vars. Below is the sample rough code to explain:=
file1.py
def method1():
print 'hello world'
file2.py
globals = file1.__dict__
execfile(file1.py, globals, locals);
# locals['method1'] has method up from file1.py. One can even execute it by doing locals['method1']();
I want similar method in PHP, where I can read other PHP file and get methods in a variable. Is this even possible
You can define class in source PHP organized in namespace
eg:
source code
vendor\vendorname\helpers.php
<?php
namespace vendor\vendorname\helpers;
class TestHelper
{
public static function yourClassFunc ($param)
{
/** your code **/
}
}
and use where you need simply declarinng
other php source (eg atest.hp)
use vendor\dfenx\helpers\UIHelper;
...........
echo UIHelper::yourClassFunc( $param);
Related
I'm using a plugin in wordpress which requires each shortcode I define to be used to extend a function. I've set up my code in a way which means it's going to be difficult to go back and extend all the classes. For example, the plugin expects this:
class WPBakeryShortCode_myCode extends WPBakeryShortCodesContainer {}
I want to replace 'myCode' with a variable, because I have all the possible codes stored in an array. so I want to have
class WPBakeryShortCode_$customCode extends WPBakeryShortCodesContainer {}
i.e. using a variable to define a class which then extends another class. Every way I've tried this I've gotten an error. Any ideas how I can do this?
You cant instantiate one at runtime from a variable.
There's no recommended way to dynamically do that because the generated class has to be instantiated before the variable is evaluated
Using Eval is bad practice and its not recommended, if you want to know how here is example
akshay#db-3325:/tmp$ cat test.php
<?php
class A { }
class B {
function iamB(){
print "From B".PHP_EOL;
}
}
$myvariable = 'B';
$code =<<<EOF
class C extends $myvariable {
public static function bar(){
print "not recommended".PHP_EOL;
}
}
EOF;
eval( $code );
C::iamB();
C::bar();
?>
Output
akshay#db-3325:/tmp$ php test.php
From B
not recommended
So mainly this is caused by my code structure:
File1.php
use \Class1 as Blah;
require 'File2.php';
File2.php
$closure = function ($arg) {
return new Blah($arg);
};
Somehow the part behind ... as isn't recognized after using require().
Namespace aliases are only valid within the file in which you write the use statement. The aliases do not transfer across file boundaries. If you want to use Class1 as Blah within File2.php, you need to put that statement at the top of File2.php.
is not recognized by the code - Slim is not a compiler, it is a framework.
The code will execute to the PHP compiler standards, therefore using the default.
use SomeNamespace\SomeClass as SomePrefix;
Will work no matter what you're trying to achieve. The require_once() method loads your PHP file holding the class, which I assume doesn't have a custom namespace since you're targeting the \ directory.
If you're working inside a class then this might be your issue, your code should run similar to this after using the require_once() to load in the ParseMarkdownClass file.
use ParseMarkdownClass as Md;
class SomeClass
{
public $container;
public function __construct()
{
$this->container = (object)['m' => function() {
return new Md();
}];
}
}
(new SomeClass())->$container->m()->...
I have got a PHP file in which there are some functions (not included in a class). I am using PHPUnit to testing. When I try to generate in a simply way a test file from a file containing functions, the log says:
Could not find class...
Is there any possibility to test functions which are not methods?
Yes, you can with something like this:
includes/functions.php
function my_function() {
return true;
}
tests/MyFunctionTest.php
require_once '../includes/functions.php';
class MyFunctionTest extends PHPUnit_Framework_TestCase
{
public function testReturnValue()
{
$return_value = my_function();
$this->assertTrue($return_value);
}
}
So as long as your function is within scope you can call it from your test method just like any other PHP function in any other PHP framework or project.
If I'm right then PhpUnit works only with classes hence methods. So just convert them into the methods for testing purpose. Shouldn't be hard.
I'm using the Markdown library for PHP by Michel Fortin. Setup is easy and it works great like this:
include_once "markdown.php";
$my_html = Markdown($my_text);
However, I have a class in which I want to pass stuff and 'Markdown' it, like so:
class Test
{
public function showMarkdown ($text)
{
return Markdown($text);
}
}
Obviously, my class is much larger than this, but this is what it boils down to. In my main script I do:
include_once "markdown.php";
$test = new Test();
echo $test->showMarkdown($text);
This returns an error, saying the function 'Markdown' is undefined. That seems obvious, because it's not within the class and I haven't used a scope operator. But when I put the include inside my class and use $this->Markdown or self::Markdown the function is still undefined. I figured that the Markdown function can't be defined inside another function.
So, how can I solve this? I need to do the include, which loads the Markdown function (and the rest of its family) but I want to be able to use it from within my classes.
Thanks for your answers/ideas.
Your example code calls a free function called Markdown (which presumably is defined in markdown.php). You simply need to put the include in the same file as your Test class.
After doing this, you will still call Markdown as a free function, and not as an instance ($this->Markdown) or static (self::Markdown) method.
write
function showMarkdown ($text)
in place of
public function showMarkdown ($text)
and
echo $test->showMarkdown("Hello World");
In php suppose there is a class named as 'test_class' in test.php with two methods: method_1 and method_2.
This is my test.php file:
class test_class
{
function method_1
{ ....... }
function method_2
{ ....... }
}
Now, using the "exec" command from other php file, I want to run only the method_1 in the test_class.
I want to know how this can be achieved?
You can do this instead:
include_once 'test.php'; // correct path if needed
$test = new test_class();
// call method1
$test->method_1();
Have you read the manual?
exec isn't meant to call methods, it's used to:
exec — Execute an external program