Can classes have bitshifted values defined as class constants / static variables ?
I want to implement a permission system similar to the one described here http://www.litfuel.net/tutorials/bitwise.htm (sorry best example I could find from a quick google)
For example ive tried this
class permissions {
const perm1 =1;
const perm2 =2;
const perm3 =4;
//--CUT--
const perm24 =8388608;
const perm25 = perm1 | perm2;
} which gives
syntax error, unexpected '|', expecting ',' or ';'
and the preferred way
class permissions {
static $perm1 =1<<0;
static $perm2 =1<<1;
static $perm3 =1<<2;
//--CUT--
static $perm24 =1<<23;
static $perm25 = $perm1 | $perm2;
}
which gives
syntax error, unexpected T_SL, expecting ',' or ';'
The latter way works outside of a class environment eg
$perm1 =1<<0;
$perm2 =1<<1;
$perm3 =1<<2;
//--CUT--
$perm24 =1<<23;
$perm25 = $perm1 | $perm2;
echo $perm25;
Giving the expected 3 (2+1) (or 2^0 + 2^1)
What is the best way to define this in a class ?
Quoting from the docs:
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
bitwise or logical operations qualify (like mathematical operations) as not permitted
Related
I have following class:
class RemoteService {
const BASE_URL = "...";
const SEARCH_URL = self::BASE_URL . "search";
const MIN_PAGE_SIZE = 0;
const MAX_PAGE_SIZE = 100;
const ERROR_INVALID_PAGE_SIZE = sprintf('page size must be between %d and %d', self::MIN_PAGE_SIZE, self::MAX_PAGE_SIZE);
...
}
I can use BASE_URL while initializing SEARCH_URL. However, I cannot use MIN_PAGE_SIZE and MAX_PAGE_SIZE in sprintf() to initialize ERROR_INVALID_PAGE_SIZE and get the following error:
Fatal error: Constant expression contains invalid operation
What is the reason for this? Is concatenation as in first part only way to overcome this?
You have to use concatenation. The documentation says:
The value must be a constant expression, not (for example) a variable, a property, or a function call.
So you can't call sprintf().
I am a PHP programmer trying to learn more of the theory behind PHP, but having trouble connecting the dots between PHP and C. For example, is the arrow operator exactly the same in PHP and C?
Here's what I came up when I researched it:
In C, -> is just an alias, a->b is the same as (*a).b. The arrow operator is just dereferencing a pointer so you interact with the address variable.
In PHP, -> is a reference. It "references the attributes of an instantiated object" (Unknown). But is that the same thing as C?
Note: Today, I learned what pointers are in C.
In PHP, -> is used to access members of a class. C does not have classes.
The closest thing is a struct.
In PHP
class Animal {
public $color;
public $age;
}
$fido = new Animal;
$fido->color = 'white';
$fido->age = 3;
$kitty = new Animal;
$kitty->color = 'brown';
$kitty->age = 5;
// output
echo 'Fido is ' . $fido->color . "age=". $fido->age . "\n";
echo 'Kitty is ' . $kitty->color . "age=". $kitty->age . "\n";
Output is:
Fido is white age=3
Kitty is brown age=5
You can do something similar in C using structs. It's a bit more involved.
Excuse my C. It's quite rusty
struct Animal {
int age;
char color[50];
};
int size = sizeof(struct Animal);
struct Animal * fido = malloc(size);
struct Animal * kitty = malloc(size);
fido->age = 3;
strcpy(fido->color, "white");
kitty->age = 5;
strcpy(kitty->color, "brown");
printf("Fido is %s age=%d\n", fido->color, fido->age);
printf("Kitty is %s age=%d\n", kitty->color, fido->age);
Unless you really want to get into the underlying details, don't overthink PHP references. What that means is that they don't pass around the actual values when doing function calls etc.
Don’t try too hard to find equivalence between the two languages. Their semantics are simply too different, so this will fail.
That said, the dereference operator -> in PHP was likely chosen to visually resemble the member access operator -> in C, and the semantics are somewhat similar, in that both allow you to access a member of a dereferenced object.
I’m not sure what you mean by “In C, -> is just an alias”: The C language has a concept of “alias”, but it’s completely unrelated with the topic at hand.
Rather, -> is an operator, and the expression a->b is defined to be equivalen to (*a).b, as you said correctly. But unlike you said, the object doesn’t need to be allocated on the heap, it can be anywhere in memory. Consider the following:
struct foo {
int i;
};
int main(void) {
struct foo f = {42};
struct foo *pf = &f;
printf("f.i = %d\n", pf->i);
}
Here, pf->i is equivalent to f.i (or (*pf).i). In no case is i allocated on the heap.
In php arrow -> is used to access function of a class.
class A{
function funA(){
return "Hello World";
}
}
$object1 = new A();
$object1->funA;
Object will be
Hello World
You can also access nested objects by arrow operator in PHP.
We will convert string to object. Here is my string:
{
"id":"123456",
"msg":"Have a Nice Day",
"is_active":false,
"total_count":1
}
IF i encode it to JSON
$obj = json_decode($json, false);
I can easily get object value by -> operator
$obj->msg;
OutPut will be
Have a Nice Day
You can do similar in C by using structs.
Example:
const USERNAME_MIN_LENGTH = '2';
private $uesrname_error_message= 'ERROR: Max. username length is ' . USERNAME_MIN_LENGTH;
I get this error with the code provided above:
Parse error: syntax error, unexpected '.', expecting ',' or ';'
The error occurs for the line in which the $test variable is defined.
I'm using PHP 5.5.12 version.
Why?
Expressions in property declarations are not (yet) supported by PHP. Some expressions will be allowed from PHP 5.6 onwards. You can read more about it on PHP's wiki.
Your specific example should work in PHP 5.6.
You need to define variable $test inside the constructor like this:
class yourClass{
private $test;
function __construct(){
const USERNAME_MIN_LENGTH = '2';
$this->test = 'Max. username length is ' . USERNAME_MIN_LENGTH;
}
}
This only works in PHP 5.6:
private $test = 'Max. username length is ' . USERNAME_MIN_LENGTH;
Currently i am learning php. Here I have a confusion this is my php code
class OBJECT_ENUM
{
const USER = 10;
const POST = 30;
const SECURE_REQUEST = 40;
}
class OPERATION_ENUM
{
const INSERT_USER = OBJECT_ENUM::USER + 1; // <- here it gives an error
const SEND_MAIL = OBJECT_ENUM::USER + 2;
const LIKE_POST = OBJECT_ENUM::POST + 1;
const INSERT_POST = OBJECT_ENUM::POST + 2;
const ENCRYPT = OBJECT_ENUM::SECURE_REQUEST + 1;
}
error message:
Parse error: syntax error, unexpected '+', expecting ',' or ';' in /var/www/workspace/6thAssignment/include/tempCall.php on line 15
I just don't understand why this error occurs.?? cany anybody explain me.??
Thank you in advance
ORIGINAL ANSWER:
As you can see in http://www.php.net/manual/en/language.oop5.constants.php:
The value must be a constant expression, not (for example) a
variable, a property, a result of a mathematical operation, or a
function call.
UPDATED:
From PHP version 5.6 now it is possible to use expressions in constants.
I think you can not do mathematical operation to be assigned to a const variable. Try changing
const INSERT_USER = OBJECT_ENUM::USER + 1;
to
$INSERT_USER = OBJECT_ENUM::USER + 1;
I believe expressions (like $a + 1) are not allowed in constants definitions, so there is why you are getting that error.
At the moment this is not allowed by PHP. There was an RFC (Request for comments) to get this added to the language:
https://wiki.php.net/rfc/const_scalar_expressions
however this was withdrawn as the author of the RFC has left the internals development team. So I suspect this may not happen any time soon but that's not to say that this won't come back in some form or another at a later date.
I'm trying to create a little enum and I'm just stuck: Why doesn't this work?
class.LayoutParts.php:
<?php
class LayoutParts {
const MAIN = 1;
const FOOTER = 2;
}
?>
class.SupportedLayouts.php:
<?php
class SupportedLayouts {
const MAIN = LayoutParts::MAIN;
const MAIN_FOOTER = LayoutParts::MAIN.LayoutParts::FOOTER;
}
?>
It results the following message:
Parse error: syntax error, unexpected '.', expecting ',' or ';' in /*****/class.SupportedLayouts.php on line 4
Thanks for your help!
Regards, Flo
. is an operator, making LayoutParts::MAIN.LayoutParts::FOOTER; a statement, which isn't allowed in a const or property declaration.
See here
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.