I get a parse error when trying to use a name space inside my own function
require('/var/load.php');
function go(){
use testClass;
$go = 'ok';
return $go;
}
echo go();
I get a parse error when trying to use a name space inside my own function
require('/var/load.php');
function go(){
use testClass;
$go = 'ok';
return $go;
}
echo go();
You can't break/continue a loop outside a function, from within a function. However, you can break/continue your loop based on the return value of your function:
function myFunction(){
//Do stuff
return $foo === 'bar';
}
foreach($x as $y) {
if(myFunction()) {
continue;
}
//Do stuff
echo $output . '<br>';
}
Second option is better if you want to implement some kind of polymorphism or to completely change function's logic. Something like this.
namespace Nkamm;
// like a PHP6
function strstr($needle, $haystack)
{
return strstr($haystack, $needle);
}
var_dump(strstr('t', 'Long test'));
Result:
string(4) "test"
But I would not like to do such "overloads" because that will cause the mess (until they are strictly documented in project). Hence there is no sense to overwrite existing functions.
Summing up: keep your functions in your namespace, use global functions without any backslashes.
Inline script is run via the eval()
function and any variables declared in the global scope are only accessible via the $GLOBALS
superglobal ($GLOBALS['var']
) or by initializing them with the global
keyword (global $var
).
I should note that if you're using dompdf 0.6.x you no longer have to specify the image type. You also appear to have the parameters backwards. The method looks for image path, x, y, width, then height.
The following should work:
<?php
<script type="text/php">
$pdf->page_script('
if ($PAGE_NUM >= 2) {
$pdf->image(' . $GLOBALS['var'] . ',0,0,25,500);
}
');
</script>
?>
Minor note: Since you're creating a page script I concatenated the image path variable with the rest of the script string. You can include that variable reference as part of the script rather than concatenate, but then you're performing additional look-ups on the variable every time the script is run. Though I must admin this is a minor performance consideration in the grand scheme of where this is running.
PHP 5.6 will allow to import functions with the use
keyword:
namespace foobar {
function baz() {
echo 'foo.bar.baz';
}
}
namespace {
use function foobarbaz;
baz();
}
See the RFC for more information: https://wiki.php.net/rfc/use_function
From Scoping rules for importing
So you should put like this, use should specified at the global level
Check the example 5 in the below manual Please refer to its manual at http://php.net/manual/en/language.namespaces.importing.php