I have a class that looks like this:
utils/Result.php
<?php
class Result
{
public static function ok()
{
echo "OK";
}
}
If I create the following script
./sandbox.php
<?php
require_once("utils/Result.php");
print_r(Result::ok());
And run it with php sandbox.php it works fine. But if I do the following: cd test && php ../sandbox.php it gives me the following error
PHP Fatal error: Call to undefined method Result::ok() in /mnt/hgfs/leapback/sandbox.php on line 5
Now, realize that the require statement seems to be working. If I add a property to the Result class, and use print_r on an instance of it, it looks right. But the static methods disappear. I'm very confused. I'm running php 5.2.6.
Do you have a 'utils/Result.php' file in the directory you have changed to (test)? If yes, it will be included instead of the original file.
Related
I have a php class that uses "include" to load some html and php from a file. Within that file I want to access the class object that included the file, but I keep getting "Fatal error: Call to a member function makeSizesSelect() on a non-object ..."
I've tried both include and require, I've tried declaring globals, I've tried everything I can think of and everything I've so far found on SO. Nothing seems to allow the file I include to have php code access the object that included it.
Any ideas?
Here's a few snippets ...
The class file:
class cdf {
public $version = 001;
public function cdf_shortcode( $atts,$content )
{
$this->slog( 2,"shortcode() case: show" );
require( 'templates/container.php' );
}
}
And the required file container.php contains the following (amongst other stuff):
<?php
echo "version = ".$this->version;
?>
I then try to use the object:
$cdf = new cdf();
$cdf->cdf_shortcode( null, null);
The line $this->slog( 2,"shortcode() case: show" ) works. It runs that function (which I haven't included in this snippet) just fine. But then the file I require (or include) cannot use $this to access the object. I'm at a loss. :-(
All I want to do is access within the included file, the variables and methods in the class that included the file ...
Sorry, some added information. I'm not sure if this makes any difference. The code above is all part of a WordPress plugin.
Curious issue with a curious solution. I finally found the answer over here:
Possible to access $this from include()'d file in PHP class?
I tried all the obvious solutions this poster tried (globals, casting to another variable, etc) with the same lack of success. Turns out, just changing the file extension from .php to .tmpl fixed the issue, and my included file can now access the object that included it. Weird. (Of course, the downside now is that my IDE doesn't colour my code for me. :-( )
Thanks for your suggestions guys.
In the file you included you need to instantiate the class.
<?php
$yourClass = new cdf();
echo "version = ".$yourClass->version;
?>
When you want to access a function in a class, you need to instantiate the class first otherwise you wont have access to anything inside of it.
Also make sure the file you are including wont be included anywhere else where the class cdf doesn't exist because that will result in an error.
The variable $this can only access methods, variables, etc. only if they are in the same object.
Update based on your answer that seems to have worked:
Example.php
<?php
echo $this->returnString();
echo $this->randomVariable;
File.php
<?php
class IncludedClass
{
public $randomVariable = 123;
public function returnString()
{
return "some random string";
}
public function meh()
{
require_once('Example.php');
}
}
$meh = new IncludedClass();
$meh->meh();
I have a a class stored in path plug/PHPDocumentParser/DocumentParser.php:
namespace LukeMadhanga;
class DocumentParser {
static function parseFromString($string) {
// do stuff
}
}
I want to call the class and function. I run this in a file that's stored at the base folder:
include_once("plug/PHPDocumentParser/DocumentParser.php");
$docObj = new DocumentParser();
$docText = $docObj->parseFromString('hello world');
I receive this error:
Fatal error: Class 'DocumentParser' not found
I am pretty sure the problem is how I call the class, correct?
You are calling static function in wrong way. Try
DocumentParser::parseFromString()
Also use require_once, you will know if it was included correctly. (maybe path is wrong.)
Edit : Ok, you added namespace now - it should be \LukeMadhanga\DocumentParser::parseFromString() thats also why you dont get instance of DocumentParser using new.
Of course you can always add use keyword at top of your file to include your namespace.
I am relatively new to unit testing, so maybe someone can help me out here.
Problem
The following error message appears when executing the PHP unit test in the terminal:
Fatal error: Call to undefined function Path\to\missing_function() in /path/to/file.php on line 123
Normally, I would now create a dummy object using the getMock(originalClassName) function and then predefine what should be returned for missing_function() but sadly the function is not placed in any interface/class like all other functions I tested up to now.
Anyone got an idea here? Cheerio!
Generally speaking, you would just include the file with the function and have it get executed. Then you just make sure that after your code was executed, what was supposed to happen happens. You are more concerned with the outcome of the code that you are testing rather than the process of how your code is executed.
Though, if you need to mock a function there is a way using namespaces (need PHP 5.3+). In your test file, you can place a "mock" function that is in the same namespace as your code. When the function gets called php looks in the current namespace first and will find your replacement function. In the normal running of your code, it will proceed to the global namespace to call your function.
So you code would end up like this:
Your class:
namespace Foo;
class SUT {
public function bar() {
return baz();
}
}
Your test:
namespace Foo;
function bar() {
return 'boz';
}
class SUTTest extends PHPUnit_Framework_TestCase {
public function testBar() {
$sut = new SUT();
$this->assertEquals('boz', $sut->bar());
}
}
I am getting an error in PHP:
PHP Fatal error: Call to undefined function getCookie
Code:
include('Core/dAmnPHP.php');
$tokenarray = getCookie($username, $password);
Inside of dAmnPHP.php, it includes a function called getCookie inside class dAmnPHP. When I run my script it tells me that the function is undefined.
What am I doing wrong?
It looks like you need to create a new instance of the class before you can use its functions.
Try:
$dAmn = new dAmnPHP;
$dAmn->getCookie($username, $password);
I've not used dAmn before, so I can't be sure, but I pulled my info from here:
https://github.com/DeathShadow/Contra/blob/master/core/dAmnPHP.php
How to reproduce this error:
Put this in a file called a.php:
<?php
include('b.php');
umad();
?>
Put this in a file called b.php:
<?php
class myclass{
function umad(){
print "ok";
}
}
?>
Run it:
PHP Fatal error: Call to undefined function umad() in
/home/el/a.php on line 4
What went wrong:
You can't use methods inside classes without instantiating them first. Do it like this:
<?php
include('b.php');
$mad = new myclass;
$mad->umad();
?>
Then the php interpreter can find the method:
eric#dev ~ $ php a.php
ok
I am including one PHP script into another using PHP's require_once() method. This script contains a class, TemplateAdmin, which instantiates itself right after the script, like this:
class TemplateAdmin {
// Class body...
}
$templateAdmin = new TemplateAdmin();
This was working fine for a while. However, I have adopted a new importing technique to include classes and packages. I have tested this new technique, and it works! However, for some strange reason, none of the methods in any of the classes I import are there when I need them. However, it seems as though the instance variables are still there.
For example, when a class with this absolute path is called:
require_once("C:\wamp\www\wave_audio\system\server\templates\TemplateAdmin.php");
... I get this error in the call stack:
Fatal error: Call to undefined method stdClass::top() in C:\wamp\www\wave_audio\cms\index.php on line 189
This error is referring to my use of the top() method inside of the TemplateAdmin class.
Does any one have any idea as to why this is happening??? If this helps, I have been using require_once() all along, I am running PHP 5.3.5 on a Windows XP Media Center machine.
Thank you for your time!
Assuming you dont want to use globals here is one way that only requires a few changes.
TemplateAdmin.php:
class TemplateAdmin {
// Class body...
}
return new TemplateAdmin();
Return include once in import:
function import($classes) {
//Convert ECMAScript style directory structures to Unix style
$address = str_replace(".", "/", $classes);
$address = INSTALL_ROOT . "system/server/" . $address . ".php";
if (file_exists($address) && is_file($address)) {
return require_once($address);
} else {
die(""" . $classes . "" does not link to an existing class");
}
}
Assign the variable:
$adminTemplate = import('templates.TemplateAdmin');
I have a feeling your php error message is accurate. I know on your stripped down version, you pieced it together how you're sure it's setup but it's obviously not a direct copy/paste since it's like:
class TemplateAdmin {
public function top() {
//The "top" method...
}
}
So, the error message says that the method "top" is not defined. If it were not including your file properly, it would tell you that the class you instantiated doesn't exist. Either that method does not exist in the class you think it is, or the method has been unset somewhere in that object instance. Trust your error message.