I'm trying to make the following work:
function flosoftdedicated_api_init() {
require_once 'resources/vendor/autoload.php';
use \Ovh\Common\Ovh;
....
$ovh = new Ovh($config);
return $ovh;
}
function flosoftdedicated_ClientArea($params) {
global $ovh;
$ovh = flosoftdedicated_api_init();
....
}
but I get the error:
Parse error: syntax error, unexpected T_USE in ..../flosoftdedicated.php on line 35
Line 35 being the use statement.
Is it not possible to use a namespace in a function? Do I need to assign the same namespace?
It's because you cannot declare it from within a function. From PHP: Using Namespaces:
The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.
You'll have to move it outside of any function or class.
The "use" keyword can not be declared inside the function or method.
from php.net http://www.php.net/manual/en/language.namespaces.importing.php#98908
Related
I am sure I've seen once example of assigning $this to another variable, single-letter to make the code typing quicker.
class Dclass {
private $d = $this;
}
But it's not working, is throwing me an error :
Fatal error: Constant expression contains invalid operations in ..
Okay, so from the another post How to handle class variable without $this?
"$this is only available inside functions within the class." #FrankerZ
So this kind of assignment should be done inside the class constructor, or other function?
Other than those described by http://php.net/manual/en/reserved.keywords.php, are there any method names I shouldn't use? For instance, can I make a method public function echo($x) and use it as $this->echo('Hello');?
EDIT> Evidently, I can't use echo because it is a language constructs, but could use min. Following doesn't result in an error.
<?php
class myClass {
public function min($x){echo(min($x));}
}
$obj=new myClass();
$obj->min(array(5,3,6,3));
?>
If you'd bothered actually trying:
php > class foo { function echo($bar) { print($bar); } }
PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting identifier (T_STRING) in php shell code on line 1
You can't use language construct names in a classes regarding namespaces, while you can use built-in function names in namespace. A namespace in this case is a method of a class.
$foo = new Foo();
$foo->array_key_exists(); // Good
$foo->isset(); // Fatal error, since isset() a language construct
$foo->include(); // Also fatal error
The reason you don't get a fatal error when using min() is because its a built-in function.
Generally it's considered bad practice using built-in names in your own implementations because that might confuse another developers that will be reading your code in future.
I use dirname(__FILE__) in includes in php scripts but the other day I included it as part of a string and it caused an error. Any ideas?
THe line was
private $errorfile = dirname(__FILE__).'/../../../error_logs/error.log';
or
private $errorfile = '/../../../error_logs/error.log';
error_log($message,3, dirname(__FILE__).$this->errorfile);
and it caused an error such as
PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home2/futsalti/public_html/_futsal-time-v4.9/public/scripts/php/databaseClass.php
PHP Parse error: syntax error, unexpected ';' in /home2/futsalti/public_html/_futsal-time-v4.9/public/scripts/php/databaseClass.php
EDIT:
Ok, just came to me... Maybe the question should be can I use dirname(__FILE__) inside a class?
Property default values must be a fixed constant value; you can't use dynamic values, variable, concatenated strings or function calls in the default value for a property.
You can use a constant, and as I noted earlier in a comment above, __DIR__ is a valid replacement for dirname(__FILE__).
Therefore, you could do this:
class myClass {
public $path = __DIR__;
}
That works, but you can't add anything else to it in the initial declaration, so although it gets closer, it doesn't really answer your question.
If it needs to be anything more than that, you'll need to define it in the code rather than the default value. I suggest declaring the variable as an empty string or null in the property declaration, and using your __construct() function to populate the value as required.
class myClass {
public $errorFile = null;
public function __construct() {
$this->errorFile = __DIR__ . ''/../../../error_logs/error.log';
}
}
Hope that helps.
Yeah, you can use dirname(__FILE__) inside a class, but not directly. Assign the path to the $errorfile in some function called before using that path.
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated. source
You're trying to concatenate two strings together as the default value for a defined property within your class. You basically can't use any operations at all.
For example, this code would throw the error you're experiencing:
class Foo() {
private $bar = "string1"."string2";
}
This code would also throw the same error:
class Foo() {
private $bar = 1+1;
}
This code would not throw an error:
class Foo() {
private $bar = "string1string2";
}
A possible workaround to your problem might be creating a method which returns the error log.
class Foo() {
function getThing() {
return "string1"+"string2";
}
}
yes you can, you can use it anywhere but you just cant use it to set the default value of a property in a class. actually you can't with any function in general. if you would like to set that as a default value each time you instantiate the class, then place it in the constructor
I get a parse error when trying to use a name space inside my own function
require('/var/load.php');
function go(){
use test\Class;
$go = 'ok';
return $go;
}
echo go();
From Scoping rules for importing
The use keyword must be declared in the outermost scope of a file (the
global scope) or inside namespace declarations. This is because the
importing is done at compile time and not runtime, so it cannot be
block scoped
So you should put like this, use should specified at the global level
require('/var/load.php');
use test\Class;
function go(){
$go = 'ok';
return $go;
}
echo go();
Check the example 5 in the below manual
Please refer to its manual at http://php.net/manual/en/language.namespaces.importing.php
From the manual:
The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations.
From what I gather a lot of people are getting this when including a separate function file and trying to use the static method inside that function.. For example in index.php
namespace foo/bar
require('func.php')
f();
and in func.php
function f() {
StaticClass::static_method();
}
you simply need to declare namespace foo/bar in func.php (same like how you declared it in index.php) so instead of the above it should look like:
namespace foo\bar
function f() {
StaticClass::static_method();
}
to avoid errors like:
Fatal error: Uncaught Error: Class 'StaticClass' not found in func.php
It's obvious now but I was confused why func.php does not carry over the namespace declaration inside the file that 'requires' func.php
Is it possible to define a global function from within a PHP namespace (within a file that has a namespace declaration)? If so, how?
<?php
namespace my_module;
# bunch of namespaced functions
# ...
# then I want to define a global function my_module()
# that is an alias to my_module\mymodule()
It's possible but aside from being bad design, you will have to enclose your code within brackets for each namespace and won't be able to use namespace my_module; Instead it will have to be namespace my_module { ... }.
Example:
namespace my_module {
function module_function()
{
// code
}
}
namespace {
// global namespace.
function my_module()
{
// call your namespaced code
}
}
Functions defined in 'included' files have global scope.
So ( I haven't verified this ) you could put your function declaration in a small file, and include it.
Not quite what was envisaged in the question.
The other way to do it would be to create a local function, and in it use the 'global' keyword, and assign a closure to a global variable - which could then be used 'as a function' wherever you wanted it.
I've certainly done this for ordinary variables, and I see no reason it should not work for closures.