0

I'm trying to access php class from another file:

$john = new User();
$john->setPassword('123');
var_dump($john);

this class code works fine with all exceptions, when pass length < 8

class User
{
...
public $password;
const MINCHARS = 8;
...
public function setPassword($string){
if(strlen($string) < self::MINCHARS){
  throw new Exception("pass must be at least ". self::MINCHARS . 'chars long');
  }else{
    $this->password = hash('sha256', $string);
  }
}

but if I change if statement to (without self::)

if(strlen($string) < MINCHARS) 

and access the class as usual

flow goes silently to else clause without any error or exception and in browser I see pretty good string

object(User)#1 (2) { ["email"]=> string(10) "john@email" ["password"]=>   string(64) "a5b432ee0307be7fa23aa00461f54eee34ba9d45251b5504567d37a8da339dff" }

why "if" thinks that it is true in this case?

version PHP is:

PHP 7.0.8-0ubuntu0.16.04.3 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.8-0ubuntu0.16.04.3

works on embedded server

php -S localhost:8080 
Pavel L
  • 1,857
  • 3
  • 17
  • 23
  • 2
    Might have been a typo while writing message but you have MINCHAR in the declaration and MINCHARS in the if statement – Katie Dec 14 '16 at 12:02
  • is it possible that you've also defined `MINCHARS` somewhere outside that class? – Jeff Dec 14 '16 at 12:06
  • 1
    Katie , thanx, there are both MINCHARS in source, question edited. – Pavel L Dec 14 '16 at 12:07
  • Jeff , no, this is standalone small piece of code, I'm just meet PHP and training around – Pavel L Dec 14 '16 at 12:09
  • Do you have error reporting turned on, because I see a PHP Notice being thrown when using just MINCHARS without the static – Katie Dec 14 '16 at 12:11
  • Katie , yes, I have `` in file, besides I see `[Wed Dec 14 13:53:43 2016] 127.0.0.1:38696 [200]: / - Uncaught Exception: password must be at least 8chars long in /home/pl/work/php/lib/User.php:26 Stack trace: #0 /home/pl/work/php/index.php(19): User->setPassword('123456') ` in console when password is too short and `self::` is present – Pavel L Dec 14 '16 at 12:16

2 Answers2

2

This is what I see when I run your code without the self::

PHP Notice: Use of undefined constant MINCHARS - assumed 'MINCHARS' in /media/sf_thalia/sandbox/iterators/staticstuff.php on line 10 PHP Stack trace:

which I read as it is turning MINCHARS into a string and then comparing.

Here is another post about it: What does the PHP error message "Notice: Use of undefined constant" mean?

The answerer also says that PHP bizarrely turns it into a string. I have no idea why it wouldn't at least try to use the constant in the class if it is searching for things to do.

You are going to want to keep using static:: or self::.

Community
  • 1
  • 1
Katie
  • 2,594
  • 3
  • 23
  • 31
  • Katie , unfortunately, I don't see anything about "notice" in browser, only in console when code is right and condition is wrong – Pavel L Dec 14 '16 at 12:27
  • When I run it in either the command line or the browser I see the notice, I currently have my php.ini set at E_ALL for error reporting. – Katie Dec 14 '16 at 12:31
  • 1
    When I tried the error reporting code you have, it does not show the NOTICE, you should change it to the error code shown in @Vasil Shaddix answer – Katie Dec 14 '16 at 12:38
  • thanx, I've done this and now I see notice too. But I think that " Use of undefined constant" - is an **error**, not notice, because it happens in key area - "if" statement and codeflow runs wrong direction. – Pavel L Dec 14 '16 at 12:50
  • Either way, the proper way to access the constant is through static:: or self:: – Katie Dec 14 '16 at 12:59
2

When you try to access a constant in php without anywhere defining it, it will always return the name of the const as string.

From the php manual you can see the constant is transfered to integer (most likely 0) and then compared with the strlen($string).

It will always return true, as long as the string has 1 character.

Remember to have php error reporting on at all time.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

so you will see these errors on the fly and fix them right away. You can always stop the error reporting in production mode.

Vasil Rashkov
  • 1,818
  • 15
  • 27
  • Vasil Shaddix , I answered above to Katie , that I have "error reporting" piece of code at the top of file, maybe this need to set on server side? – Pavel L Dec 14 '16 at 12:24