Showing posts with label object-oriented. Show all posts
Showing posts with label object-oriented. Show all posts

Tuesday, July 29, 2008

Clarification of Encapsulation in PHP - OOP

PHP Developer - Boston, MA | Object Oriented Programming

Here is a demonstration of what is happening to an object member while changing it. This should help you clarify how PHP handles pointers:


class A {
public $val;

public function __construct( $val ) {
$this->val = $val;
}
}

class B {
public $val;

public function __construct( $val ) {
$this->val = $val;
}
}

$a = new A(5);

echo $a->val; // 5

echo "
";

$b = $a;

$b->val = 10;

echo $a->val; // 10

$c = clone( $a );

$c->val = 15;

echo "
";

echo $c->val; // 15

echo "
";

echo $a->val; // 15

echo "
";

$z = 1;

echo ++$z * $z++ + $z; // 7

echo "
";

echo $z; // 3

echo "
";

$m = 0;
do {
echo $m;
} while ( ++$m < 10 );


Monday, July 7, 2008

Memory Leaks in PHP

There is a huge memory leak in PHP 5.2 and earlier. When objects nest other objects within it with circular reference, the nested object is never unset when un-setting the object that is instantiating the other objects. The solution would be to upgrade to latest version of PHP 5.3 when it is stable, or un-setting all the objects that get instantiated within an object. Some more solutions are proposed at Memory Leaks in PHP. Another author looks at this problem at Memory Leaks in PHP. The author also tries to propose a solution by modifying the PHP internals and decrement the reference count. This is beyond my scope and time constraints me to do this.