Google
  Web www.spinics.net

RE: Flow of PHP testClass

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


Yes, I believe your understanding is correct... This is my understanding as well.  

In line 1 below,  you are creating a new object and using the class constructor to initialize $a and $b.
In line 2 below, you are creating a new object, with $a and $b's  default values.
In line 3 below, $a and $b are printed with the values passed through the class constructor in line 1.
In line 4 below, $a and $b are printed with default values since nothing was passed through the class constructor.

1 .  $inst1 = new TestClass(3,50);
2 . $inst2 = new TestClass();
3.  $inst1-­‐>printAB();
4.  $inst2-­‐>printAB();

-----Original Message-----
From: Rikin Parekh [mailto:rikin08@xxxxxxxxx] 
Sent: Thursday, March 29, 2012 10:57 AM
To: PHP DB; php-windows@xxxxxxxxxxxxx; phpexperts-subscribe@xxxxxxxxxxxxxxx
Subject:  Flow of PHP testClass

Hi Guys,

Given below is a PHP script. Can someone help me with the output of the code. According to my understanding the output should be 3, 50, 20, 10. Can someone elaborate on the same and provide me an explanation on the flow?

Thanks a lot in advance.

<?php
class TestClass {
var $a =20;
var $b =10;
function TestClass($a= null, $b=null) {
 if (!is_null($a))
 {
   $this-­‐>a= $a;
 }
if(!is_null($b))
 {
 $this-­‐>b=$b;
 }
}

function printAB() {
echo $this-­‐>a.” “.$this-­‐>b.”\n”;
 }
}

$inst1 = new TestClass(3,50);
$inst2 = new TestClass();
$inst1-­‐>printAB();
$inst2-­‐>printAB();
?>


-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP Home]     [PHP Users]     [PHP Install]     [PHP on Windows]     [Programming PHP]     [Kernel Newbies]     [Rice Cooker]     [Home]     [Yosemite News]     [Yosemite Photos]     [PHP Books]

Powered by Linux