Class 'MongoDB\BSON\Regex' not found [duplicate] - php

This question already has an answer here:
How to have eclipse resolve php classes in MongoDB\BSON namespace?
(1 answer)
Closed 2 years ago.
I trying example from php.net: -http://php.net/manual/en/class.mongodb-bson-regex.php
when performing a code below in php
$regex = new MongoDB\BSON\Regex ( '^Al');
$cursor = $collection->find(array('name' => $regex));
//iterate through the cursor
then php show me error Class 'MongoDB\BSON\Regex' not found
please help me how can i solve this query

You may find that only Eclipse is marking it as a problem, but when you run it on the web server it is fine.
If this is the case then your answer is here:
How to have eclipse resolve php classes in MongoDB\BSON namespace?

Related

PHP syntax - what's this? [duplicate]

This question already has answers here:
Colon after method declaration?
(2 answers)
Closed 4 years ago.
It's almost impossible for me to search for an answer to this as I don't know what this syntax is called.
I have a server running a relatively old version of PHP (5.5.38) and I'm getting parse errors when I run a file that has the following:
public function foo(): array
{
...specifically the : array suffix.
Can anyone tell me what this syntax is called (so I can research further) and which PHP version introduced it?
This was added in PHP 7, they're called return type declarations.
http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration

Dynamic prefix attribute PHP [duplicate]

This question already has an answer here:
How can I access a property with an invalid name?
(1 answer)
Closed 6 years ago.
I have a question about attributes in PHP.
I have various Class with the same attributes, but with with different prefix.
Example:
$attr->a_field;
$attr2->b_field;
So, with another Class I want to access to them.
I tried:
$field = "{$prefix}_field";
$attr->{$field}
and it works perfect. But is any other way to doing this?
I tried also with:
$attr->{$prefix}_field;
$attr->{$prefix}{"_field"};
$attr->"{$prefix}_field";
etc and who I suppose I get PHP's errors
Thanks!
You can write it directly as $attr->{"{$prefix}_field"}, as shown in the docs.
You're looking into variable variables
$attr->{$prefix."_field"}

PHP Fatal error: Can't use method return value in write context [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
if (isset( $xml->xpath('/lfm/artist/image[#size="extralarge"]')[0])) {
PHP Version 5.3 in use (was 5.7 until server dude switched it to 5.3).
On the line at the top we're getting this error:
PHP Fatal error: Can't use method return value in write context
I've read a few articles but nothing obvious springs to mind - might be a case of not seeing the wood through the trees.
Any advice on how to get rid of the error other than switching back to 5.7?
For compatibility with PHP < 5.4, update your code as follows:
$elements = $xml->xpath('/lfm/artist/image[#size="extralarge"]');
if (isset($elements[0])) {
Take a look at https://wiki.php.net/rfc/functionarraydereferencing if you're interested in learning more.

PHP Syntax Error for simple web script [duplicate]

This question already has an answer here:
Populating associative arrays
(1 answer)
Closed 7 years ago.
Running into an issue with a simple PHP script and I can't seem to figure it out. States its on line 3 and I don't see it. Need a fresh set of eyes please.
<?php
$numArr = [];
for ($i=0;$i<5;$i++) {
array_push($numArr,mt_rand());
}
echo min($numArr);
?>
It's likely how you're initializing your array. The [] syntax is only available from PHP 5.4 and above.
From the PHP manual:
As of PHP 5.4 you can also use the short array syntax, which replaces
array() with [].
You can use array () instead.

php 5.3 Fatal error Class DateTimeZone not found [duplicate]

This question already has answers here:
Problems with PHP namespaces and built-in classes, how to fix?
(3 answers)
Closed 7 years ago.
I tried, to run the following code, on PHP v5.3.13, but still getting the class not found error:
$tZone = new DateTimeZone("Europe/Amsterdam");
How can I use DateTimeZone on 5.3.13?
Try this:
$tZone = new \DateTimeZone("Europe/Amsterdam");

Categories