One of my colleagues made a post that said something like this:
In PHP, if you have two variables referring to the same value, they are the same instance.
$a="Mary";
$b="Mary";
$c="lamb"
He implies that $a and $b refer to the same instance(memory space). I am having trouble beleiving this. I know that this is somewhat true in java, but I don't think its so for php, since in php strings aren't actually immutable by principle, it would not make sense to have one instance
Further,he said, if we do unset($a) it only removes the reference of $a not the actual value.
This is ofcourse true, but proves nothing
I also tried the following code and printed both $a and $b. If they were sharing the same instance, the value of $b would have changed too.
$a[2]=3;
echo "<br/>\$a: $a<br/>"; //He3lo
echo "<br/>\$b: $b<br/>";//Hello
I would love to check the memory space of the variables, but I don't think php allows to do that. Can somebody clarify if this is true
You are referring to a concept called String interning. It seems that it is implemented in the Zend Engine since Version 5.4: "Our implementation makes the strings which are known at compile-time interned." Source.
Related
In PHP, one can write
$a="$b$c";
This concatenates the assumed strings in $b and $c into $a elegantly (that is, using minimal yet clear syntax).
Now I want to move this statement into a function. The problem is that $b and $c are naturally interpreted as being local to this function, not its caller (which can be the global code or a function). Even if the caller defines $b and $c, this new function cannot see those definitions.
I want to do something clever that requires the statement to work the same (except for the location of $a) even though it has been moved into a function.
Furthermore, I want this to be efficient. No use of extract() or debug_backtrace(). Just want to use the caller's local scope. I don't mind if I use Zend to get the caller's symbol table, or any other hack, so long as it's efficient.
Don't worry, I'm not going to use this as general programming practice (ugh), just for one specific and wonderful purpose.
I know you don't want extract, but could this help?
<?php
function foo()
{
$b = 'Butter';
$c = 'Cup';
$v = get_defined_vars();
echo bar($v);
}
function bar($v)
{
extract($v);
return $b . $c;
}
foo();
Output:
ButterCup
There is no native PHP functionality for violating function scope in this way, other than backtraces included in debugging functions and exceptions.
I'd suggest looking at the C code for debug_backtrace() and seeing if you can write an extension that implements the bit of it you need (walking up the stack one step to find the parent symbol table) but which performs better than the existing function (I've no idea if that's possible, I don't know where the overhead comes from).
However, I would also recommend thinking really really hard about what problem you're actually trying to solve, and why it's led you to want to break function isolation in such an unusual way. Maybe there's a completely different solution.
For instance, if what you want is a kind of macro, rather than a function, you could write a pre-processor (or find one that's already been written) so that the code is literally pasted into place, and you don't need to hack the symbol table.
I have been doing some research online and it appears that the answer to my question is no, but I realize there are times when I might miss something or search for something incorrectly. I know in languages like C++, when a variable is declared it can be declared as int or string. Is it possible to force this in PHP?
For Example:
<?php
(int)$var = 5;
?>
will be validated and not cause an error in PHP, but:
<?php
$var = 5;
?>
will cause an error because it was not cast as a type string, int, object, etc...
I know PHP is loosely typed so this may not be an option, however I would like to use it that way I ensure that I sanitize data appropriately and improve readability by letting others know exactly what is going on with the code. I am hoping there is a way to enforce this in the php.ini file or to load it in a script that will always be executed by my program.
Thank you for any help you can offer!
PHP is loosely typed and does not require you to declare a variable type when declaring a variable. You can’t change that behavior with a magic php.ini directive.
The benefit of having “loose typing” is that it allows for flexibility. It allows you to create dynamic applications without having to worry about the type of the variable – PHP makes this possible by not enforcing variable types.
However, if you must convert a variable into a particular format, just cast it:
$int = (int) $foo;
$str = (string) $foo;
$bool = (bool) $foo;
$float = (float) $foo;
...
There are functions to do the same, e.g. intval(), strval(), boolval() – all of them do the same task, but a function is very useful when you want to use it as a callback to another function.
I was marvelled when I tested the following code today:
$star = "Aquarius";
$star = 11;
While debugging, I observed that $star simply changes from string type to integer type. I was amazed by this functionality. In C++ for instance, this is just impossible, but in c# I considered the var variable but it's not the same.
For instance you can't do:
var dynamic = "Hello";
dynamic = 3;
I began to wonder what exactly happens at the point when I basically say $star = 11. My guess is that $star is simply reinitialized since it's being directly assigned to (but this seems weird since the interpreter already knows that a variable $star has been declared earlier). Can anyone help with some clear or official source-backed explanation?
Thanks.
In C/C++ the type is defined at compile time because of the kinds of optimization that can occur based on it.
In C# the compiler infers the type based on the context and in the compilers brain it substitutes the var keyword for the type. This is why you can not change the type after the compiler made the initial inference.
In scripting languages like PHP a variable is an entry into a Hash Map (Associative Array, a Symbol Table). This defines the namespace (and scope). The actual value part is a generic object type that stores both the value and the type.
PHP is a dynamic language, similar in spirit to Perl, Ruby, Python, or many others. C++, on the other hand, is compiled and statically typed, requiring each variable to have a type defined at compile time.
Check the PHP docs for some great insight to PHP's dynamic typing implementation:
http://php.net/manual/en/language.types.type-juggling.php
PHP is a loosely typed language. PHP converts the variable to the correct data type, according to the value.
Check this out - http://php.net/manual/en/language.types.type-juggling.php
Does PHP provide any Lazy copy concept?
My believe is that Lazy copy is not implemented in PHP(infact is it a correct terminology?) while Lazy loading can be implement on object properties by simple flag property of an object.
I came across a answer(Please see) on SO with a large number of upvote, a part of explanation seems to be completely wrong.
He is saying unless $b is not changed $a will keep only reference of $b.
$b=3;
$a=$b;
// $a points to $b, equals to $a=&$b
$b=4;
// now PHP will copy 3 into $a, and places 4 into $b
I can understand Lazy loading. Keep a flag property in object and whenever we try to get the property of an object just initialize all properties from DB. Pseudo code looks like this:
private function GetAccessor($member) {
if($this->isLoaded != true) {
$this->Load(); //initialize or copy all properties from DB - LAZY LOADING
}
....
Note: php.net also doesn't mentioned lazy copy anywhere.
My believe is that Lazy copy is not implemented
It is implemented and it is called COW (Copy-on-Write)
See:
http://www.php.net/manual/en/features.gc.refcounting-basics.php (Example 3)
http://php.net/manual/en/internals2.variables.intro.php
Well yes PHP does this. This is an optimization strategy the php interpreter does for you. The concept is also known as "copy on write".
Suppose you have a reeeaaaallly large string
$a = "lllloooooong [imagine another million characters here]";
And then you want to copy that:
$b = $a;
Then chances are, that doing this copy operation was never necessary, because either you never altered $a or $b meaning that both variables have the same value at all the time and thus you could just use $a OR $b reducing your memory consumption by 50% and saving you that copy operation.
So the PHP-interpreter will on the first operation $b = $a assume, that you probably will never change $a or $b and it will not do any copying but instead it memorizes that $b has the same data as $a. As soon as you change $b or as soon as you change $a, the interpreter's previous assumption is proven as wrong and the interpreter will do the copying after all.
But this behaviour is an operation that happens behind the scenes. You don't see it, you can't influence it directly and it does not have any effect that you must be aware of in order to code PHP. Instead you can always work as if variables would be copied immediately.
Why is it not possible to do something equivalent to this in PHP:
(Array(0))[0];
This is just for sake of argument, but it seems strange it does not allow access of anonymous objects. I would have to do something like the following:
$array = Array(0);
$array[0];
Any ideas why this is the behavior of PHP?
I read something somewhat detailed about this once and I regret not bookmarking it because it was quite insightful. However, it's something along the lines of
"Because the array does not exist in memory until the current statement (line) executes in full (a semicolon is reached)"
So, basically, you're only defining the array - it's not actually created and readable/accessible until the next line.
I hope this somewhat accurately sums up what I only vaguely remember reading many months ago.
This language feature hasn’t been inplemented yet but will come in PHP 6.
I guess the short answer is: nobody has coded it yet. I've used (and loved) that syntax in both Python and Javascript, but still we wait for PHP.
The main reason is because unlike some languages like Python and JavaScript, Array() (or in fact array()) is not an object, but an language construct which creates an inbuilt data type.
Inbuilt datatypes themselves aren't objects either, and the array() construct doesn't return a reference to the "object" but the actual value itself when can then be assigned to a variable.