Is there any reason why I can't use preg_replace inside a static function? when I move the code out of it, it works perfectly. Any ideas?
Funny, because this works fine:
class obnoxiousWeasel {
public static function callMeDoItIDareYa($omgudid)
{
return preg_replace("/(, you don\'t listen)/", '...', $omgudid);
}
}
$pieceofmymind = "ok, but what's the point, you don't listen";
$reply = obnoxiousWeasel::callMeDoItIDareYa($pieceofmymind);
echo $reply;
returns: "ok, but what's the point..."
We will need to see your code before we can tell you what is wrong. Using preg_replace inside a static function is definitely not the problem.
Edit: I actually edited the above useless function to improve it. Might as well face it, I'm addicted to refactoring.
preg_replace is a core php function and can be used at any scope.
perhaps you are using it to evaluate a class member in the static method? that would not work. but I couldn't say for sure without seeing a relevant chunk of code.
Related
I've done a search into stackoverflow but couldn't find anything.
Actually I'm working into recodding a "dirty" code done by a dev in a CakePHP 2.3 framework.
I'm not a dev myself, i'm more like a Swiss knife, i do some php but this is usually not my daily task.
:)
Let's go to the facts, i've got a controller with functions, and some functions under these functions...
Ex :
tool_controller.php
function addSomething(){
print_r($this->loadModel("db")); //it works // returns 1
function anotherFunction(){
print_r($this->loadModel("db")); //returns "Using $this when not in object context"
}
}
I'm a bit lost, searched in CakePHP docs but couldn't find anything either.
Can someone please help ?
thanks
This code would most probably be better written like this:-
public function addSomething(){
print_r($this->loadModel("db"));
$this->_anotherFunction();
}
protected function _anotherFunction(){
print_r($this->loadModel("db"));
}
I doubt you really need to nest functions for what you want to achieve. The above code should be simpler to read and is more obvious what context $this refers to.
OK people, i've solved my problem, as I told before the main function is an action after a submit, then I had a loop inside this function to treat the datas.
As i'm working in CakePHP and using the integrated functions (in this case the function "loadModel" in the "Controller" class), i had to get this function working in the nested function.
The solution was the following :
function addSomething(){
my_code_here
function anotherFunction(){
$controller = new Controller; // Had to redeclare the class
print_r($controller->loadModel("db")); // Works just fine
}
}
Thanks to all for your submissions.
Kind regards folks ;)
After that many of you suggest me to not to nest functions, I've decided to follow your advice and I've putted outside my main function, after I call back my function.
Public Function loopAction(){
//Code here
}
Public Function addsomething(){
//Code here
$this->loopAction();
}
It works perfectly and even better than before, thanks to all.
I'm just a once-in-a-while PHP developer. Now, working on a legacy application, I just hit upon the following problem, which seems very stupid. But I can't get $someString class variable to hold the right value:
class MyClass{
var $someString;
function doSomething(){
//$this->setString(); //this is effectively not called here, but later in getIframe()
$this->buildIframe();
echo $this->someString; //actually, I need someString here, but it is empty
}
function setString(){
$this->someString = "something";
}
function buildIframe(){
$content .= <iframe....>;
}
function getIframe(){
$this->setString();
}
}
$myClassInstance = new MyClass();
$myClassInstance->doSomething();
$myClassInstance->getIframe();
As far as I can see, doSomething() is called in a class context, as I did show.
What am I doing wrong?
EDIT:
I reviewed the code and I think I found whats causing this. There is an iframe embedded into the html output, which is generated at one part and called later on. So the setString() method is actually not called imediately, what I thought first but when invoking the iframe code. So thats why it's not available where I need the string output.
I guess like the code is now, there is no way to get the $someString output except inside the getIframe() method.
This code is 100% correct and working. I've checked it on PHP 5. It echos "something" in string. And it is proper behavior.
From manual:
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.
In a PHP routing file, I want to make a callback to a function called homepage_display().
The function name is in the $callback variable, and when I make the call, it doesn't work:
$callback = "homepage_display";
$callback(); // doesn't work
call_user_func($callback); // doesn't work either
homepage_display(); // works!!
Whereas this specific piece of code just above works in any snippet, the same mechanism doesn't work in one of my functions. Any idea of what can cause such a behavior? I tried removing the _ thinking it might be an encoding problem, but it doesn't solve anything.
EDIT:
To make my point absolutely clear, I added an explicit assignation $callback="homepage_display";, just before calling it as a callback function. You can see on this picture that it just does not work. Whereas calling homepage_display(); directly, does. If somebody understands something, I'm curious :)
Not working:
Working:
The answer, in one word: namespace foo\bar;.
One cannot call $callback(); when in the scope of a non-global namespace.
One has to call call_user_func(__NAMESPACE__.'\\'.$callback); instead.
It can get tricky sometimes ;)
Works fine here:
php > function foo() { echo 'foo!'; }
php > $bar = 'foo';
php > call_user_func($bar);
foo!
php > $bar();
foo!
Did you verify that your testRoute() actually returns true? If it doesn't, then your "autoloader" will never get called.
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");
I`m currently working on a script, and I have the following situation.
function somnicefunction()
{
require 'someexternalclass.php';
$somevar = new SomeExternalClass();
}
For some reason, the above breaks the function. I'm not sure why, I haven't seen much documentation in php.net regarding this, plus google returned no real results.
Does anyone have any idea ?
If you call the function more than once, you may encounter an error by trying to include the same file.
Try using require_once() instead. Other than that, there is nothing inherently 'illegal' about your example.
Your code is absolutely valid. I tested it on localhost and it works just fine. I used following piece of code:
function.php
function loadClass()
{
include_once "include.php";
new SomeExternalClass();
}
loadClass();
include.php
class SomeExternalClass {
public function __construct( ) {
echo "loads...";
}
}
Are you sure you don't have any typo there? If you aren't getting any error, it might indicate that you haven't used the function anywhere.
Try declaring the object first and then pass it as a parameter to the function:
require 'someexternalclass.php';
$somevar = new SomeExternalClass();
function somnicefunction(SomeExternalClass $somevar)
{
// Do function stuff
}