Getting an error using constants - php

I have a lot of pages, all of which require the file characters.php. This file contains constants which define many things in my website. They are defined like this, for example:
const $humanHEALTH = 1.1;
Everything works properly running it in my localhost using WAMP, but when I upload it to an online host I get this error:
Parse error: syntax error, unexpected T_CONST
I used phpinfo() on one of the pages and the PHP version is 5.2.17.
Any help would be greatly appreciated.

In PHP 5.2 it's define('constant_name', 'value');

Support for const outside of class definitions was not added until PHP 5.3, so your 5.2.x is too old to use this. See http://php.net/const

A constant must not have any $ sign at the beginning. Try const HUMAN_HEALTH = 1.1 instead.
As Marc B mentioned, const outside classes is only available up from PHP 5.3.

To add possible causes to those already mentioned: use of the $ character, and old PHP version; I leave this clarification from the manual that was useful to detect the problem in my case:
As opposed to defining constants using define(), constants defined
using the const keyword must be declared at the top-level scope
because they are defined at compile-time. This means that they cannot
be declared inside functions, loops, if statements or try/ catch
blocks.

Related

PHP class 'Ds\Map' not found

In PHP (version 7.1), I am attempting to use a MAP as opposed to a two dimensional array to handle implicit data type conversion across different data type groups. However, I am receiving the following run-time error:
Class 'Ds\Map' not found
The error occurs on this line of code:
protected $hive_data_type_group_map = new \Ds\Map();
I have checked online, but there is little documentation on Ds\Map, even on PHP's website (click here). Does anyone know how to fix this?
Data Structures is not a built-in PHP extension.
It needs to be installed before use. The installation instructions are available on php.net.
For windows
Download compiled-dll file "php_ds.dll" from https://pecl.php.net/package/ds. Place it in your dir wamp\bin\php\[your_php_version_folder]\ext and include it in your ".ini" file.
It worked for me.
the easiest way on ubuntu:
pecl install ds
Reference: https://www.php.net/manual/en/ds-deque.allocate.php

Use PDFlib in Laravel

I installed PDFlib (followed the instructions PDFlib in PHP How To) on OS X by adding extension=php_pdflib.so to my php.ini file and it is loaded properly.
(If I run phpinfo(); PDFlib is shown in the list.)
However if I try to use it with
$p = new PDFlib();
I get Parse error: syntax error, unexpected '$p' (T_VARIABLE)
What could cause this error?
EDIT
The error was simply caused by a missing semicolon...
Now I am getting a Class 'App\Http\Controllers\PDFlib' not found which is kind of obvious cause I didn't register it in the controller with use.
I thought new PDFlib() is available for global use after installing?
You're missing out the concept of namespaces. In this casePDFlib is available on the global namespace, which is \. In other words, you can either import it with use PDFlib;, or you can use it directly w/o importing it like this $p = new \PDFlib();.
now i am getting a Class 'App\Http\Controllers\PDFlib' not found.
I pretty much doubt you got PDFlib in your Controllers folder therefore it seems that your code that uses PDFlib simply lacks use to refer proper PDFlib's namespace (or you need to use fully qualified namespaces instead).
If PDFlib is not using namespace then from namespaced code youneed to use \ to reach it, i.e.:
$x = new \PDFlib();

catch php commands while executing

Are there any methods to catch the php commands instead of executing them?
There's a backdoor which is encoded in my server, I was able to get declared classes and methods names, of the backdoor, but I need to find out the real code.
I can think of 2 things.
1. Use a profiler tool.
2. In your own code define a class with the same name. Then when the backdoor class is loaded, it will give a fatal error ('class name already defined') and tell you where the other class resides. Do enable stacktraces in case the backdoor is obfuscated/eval'd/remote included.

Cannot use X as Y because the name is already in use, even though it's not

I'm using PHP 5.4, and have a PSR-0 class structure similar to the following.
A\Library\Session.php:
namespace A\Library;
class Session { ... }
My\Application\Session.php:
namespace My\Application;
class Session { ... }
My\Application\Facebook.php:
namespace My\Application;
use A\Library\Session;
class Facebook { ... }
When I try to run the application, I get the following error:
Cannot use A\Library\Session as Session because the name is already in use in My\Application\Facebook.php
Even though it's not, at least not in this file. The Facebook.php file declares only the Facebook class, and imports exactly one Session class, the A\Library one.
The only problem I can see is that another Session class exists in the same namespace as the Facebook class, but as it was never imported in the Facebook.php file, I thought it did not matter at all.
Am I wrong (in that case please point to the relevant documentation), or is this a bug?
There is a bug confirmed in PHP that may affect the behavior you see. It is supposed to fatal error, but with opcache enabled, it may still execute flawlessly.
https://bugs.php.net/bug.php?id=66773
If it still concerns you, please vote for the bug.
No, this is not a bug. As mentioned in Using namespaces: Aliasing/Importing
use A\Library\Session;
is the same as:
use A\Library\Session as Session;
So try using something like:
use A\Library\Session as AnotherSessionClassName;
The only problem I can see is that another Session class exists in the
same namespace as the Facebook class, but as it was never imported in
the Facebook.php file, I thought it did not matter at all.
Yes, it does matter. This is why you don't need to "import" classes from the same namespace. If you have conflicting names from different namespaces, you need to alias the class.
namespace My\Application;
use A\Library\Session as ASession; // choose a proper alias name here
class Facebook { ... }
I've read the thread about the issue, but I tested on many PHP versions (php 5.5, 5.6, 7.*, x32, x64, vc11, vc14, vc5). I'm using Laravel with Laragon. But, when I build up the server with php artisan serve (and open the server at http://localhost:8000) I have the problem of "the namespace that some Class was already used" and stuff.
I tested with and without opcache extension and nothing works, then I tested the virtual domain that Laragon provides and... voila, the error just disappeared and now I can work OK. I don't know what was happening, my namespaces were OK, I had an alias but the same code works in many machine without problem (AWS, local, prod, dev, etc) but only in my machine I had the problem just as I described it.
So, if someone is working with Laravel (5.1) and is having this issue, try the virtual host of Laragon.

Constant definitions not valid any more with PHP 5.4.x?

I'm trying to (in a parallel fashion, that's it, as we're still developing under PHP 5.2.x for our main applications) adapt our current applications to PHP 5.4.x (using Apache 2.4.2 with PHP 5.4.1 in our testing server) but I'm finding some warnings in our current applications when we run them in the testing server.
For example... we're using constant definition in our current applications with a function that detects the language and loads a language file containing the definitions like idiomas/lang.php, being lang a 2-digit language representation (es, us, de, and so on).
Inside each language file there's some definitions like this one:
define("IDIOMA_AF", "Afrikaans");
define("IDIOMA_AR", "العربية");
define("IDIOMA_BG", "български език");
So, when we want to output a given translated text, we do this:
<?php echo IDIOMA_AF; ?>
The message PHP 5.4.1 outputs when the application is thrown at it is the following:
Notice: Constant IDIOMA_AF already defined in D:\apache\htdocs\aplicacion\modulos\base\idiomas\es.php on line 34
Does anyone have the same problem? I would like to know a bit about your experiences, as it would be useful to know how to solve these problems. We're thinking on implementing a better system using gettext (which I think is more organized and easier to handle in the long run, but still...), although we would like to run our current applications for a while before upgrading them.
It means you are trying to define the constant twice (which is not allowed).
You could try:
if (!defined('IDIOMA_AF')) define('IDIOMA_AF', 'Afrikaans');
Or trace, and fix, why you are defining a constant twice anyway.
The function get_defined_constants() is also a useful debugging tool for issues like these.
Or, just ignore those errors: error_reporting(E_ALL & ~E_NOTICE);

Categories