jmcastagnetto           Tue Apr  1 01:39:54 2003 EDT

  Added files:                 
    /phpdoc/images      diophantine-equation.png 

  Modified files:              
    /phpdoc/en/language oop.xml 
    /phpdoc/en/reference/gmp/functions  gmp-gcdext.xml 
    /phpdoc/entities    global.ent 
  Log:
  Added some blurb on diophantine eqs to gmp_gcdext
  Added blurb on object comparisons in PHP4
  Known bug: make html does not copy the PNG from images to the correct dir
     Someone with more insight on what to change in configure.in and related
     can do the changes (don't want to break the build).
  
  
Index: phpdoc/en/language/oop.xml
diff -u phpdoc/en/language/oop.xml:1.37 phpdoc/en/language/oop.xml:1.38
--- phpdoc/en/language/oop.xml:1.37     Sun Sep 15 11:46:44 2002
+++ phpdoc/en/language/oop.xml  Tue Apr  1 01:39:54 2003
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.37 $ -->
+<!-- $Revision: 1.38 $ -->
  <chapter id="language.oop">
   <title>Classes and Objects</title>
 
@@ -774,7 +774,7 @@
     {
         echo "<br>",$this->name;
     }
-       
+ 
     function setName($name)
     {
         $this->name = $name;
@@ -935,6 +935,167 @@
     </informalexample>
    </para>
   </sect1>
+
+  <sect1 id="language.oop.object-comparison">
+   <title>Comparing objects in PHP 4</title>
+
+  <para>
+   In PHP 4, objects are compared in a very simple manner, namely: Two object
+   instances are equal if they have the same attributes and values, and are
+   instances of the same class. Similar rules are applied when comparing two
+   objects using the identity operator (<literal>===</literal>).
+  </para>
+  
+  <para>
+   If we were to execute the code in the example below:
+   <example>
+    <title>Example of object comparison in PHP 4</title>
+    <programlisting role='php'>
+<![CDATA[
+function bool2str($bool) {
+    if ($bool === false) {
+            return 'FALSE';
+    } else {
+            return 'TRUE';
+    }
+}
+
+function compareObjects(&$o1, &$o2) {
+    echo 'o1 == o2 : '.bool2str($o1 == $o2)."\n";
+    echo 'o1 != o2 : '.bool2str($o1 != $o2)."\n";
+    echo 'o1 === o2 : '.bool2str($o1 === $o2)."\n";
+    echo 'o1 !== o2 : '.bool2str($o1 !== $o2)."\n";
+}
+
+class Flag {
+    var $flag;
+
+    function Flag($flag=true) {
+            $this->flag = $flag;
+    }
+}
+
+class SwitchableFlag extends Flag {
+
+    function turnOn() {
+        $this->flag = true;
+    }
+
+    function turnOff() {
+        $this->flag = false;
+    }
+}
+
+$o = new Flag();
+$p = new Flag(false);
+$q = new Flag();
+
+$r = new SwitchableFlag();
+
+echo "Compare instances created with the same parameters\n";
+compareObjects($o, $q);
+
+echo "\nCompare instances created with different parameters\n";
+compareObjects($o, $p);
+
+echo "\nCompare an instance of a parent class with one from a subclass\n";
+compareObjects($o, $r);
+]]>
+    </programlisting>
+   </example>
+   We will see:
+   <screen>
+Compare instances created with the same parameters
+o1 == o2 : TRUE
+o1 != o2 : FALSE
+o1 === o2 : TRUE
+o1 !== o2 : FALSE
+
+Compare instances created with different parameters
+o1 == o2 : FALSE
+o1 != o2 : TRUE
+o1 === o2 : FALSE
+o1 !== o2 : TRUE
+
+Compare an instance of a parent class with one from a subclass
+o1 == o2 : FALSE
+o1 != o2 : TRUE
+o1 === o2 : FALSE
+o1 !== o2 : TRUE
+   </screen>
+   Which is the output we will expect to obtain given the comparison rules
+   above. Only instances with the same values for their attributes and from the same
+   class are considered equal and identical.
+  </para>
+  <para>
+   Even in the cases where we have object composition, the same comparison
+   rules apply. In the example below we create a container class that stores
+   an associative array of <classname>Flag</classname> objects.
+   <example>Composite objects u(o,p) and v(q,p)
+o1 == o2 : TRUE
+o1 != o2 : FALSE
+o1 === o2 : TRUE
+o1 !== o2 : FALSE
+
+u(o,p) and w(q)
+o1 == o2 : FALSE
+o1 != o2 : TRUE
+o1 === o2 : FALSE
+o1 !== o2 : TRUE
+
+    <title>Compound object comparisons in PHP 4</title>
+    <programlisting role='php'>
+<![CDATA[
+class FlagSet {
+    var $set;
+
+    function FlagSet($flagArr = array()) {
+        $this->set = $flagArr;
+    }
+
+    function addFlag($name, $flag) {
+        $this->set[$name] = $flag;
+    }
+
+    function removeFlag($name) {
+        if (array_key_exists($name, $this->set)) {
+            unset($this->set[$name]);
+        }
+    }
+}
+
+
+$u = new FlagSet();
+$u->addFlag('flag1', $o);
+$u->addFlag('flag2', $p);
+$v = new FlagSet(array('flag1'=>$q, 'flag2'=>$p));
+$w = new FlagSet(array('flag1'=>$q));
+
+echo "\nComposite objects u(o,p) and v(q,p)\n";
+compareObjects($u, $v);
+
+echo "\nu(o,p) and w(q)\n";
+compareObjects($u, $w);
+     
+]]>
+    </programlisting>
+   </example>
+   Which gives the expected output:
+   <screen>
+Composite objects u(o,p) and v(q,p)
+o1 == o2 : TRUE
+o1 != o2 : FALSE
+o1 === o2 : TRUE
+o1 !== o2 : FALSE
+
+u(o,p) and w(q)
+o1 == o2 : FALSE
+o1 != o2 : TRUE
+o1 === o2 : FALSE
+o1 !== o2 : TRUE
+   </screen>
+  </para>
+ </sect1>
  </chapter>
  
 <!-- Keep this comment at the end of the file
Index: phpdoc/en/reference/gmp/functions/gmp-gcdext.xml
diff -u phpdoc/en/reference/gmp/functions/gmp-gcdext.xml:1.2 
phpdoc/en/reference/gmp/functions/gmp-gcdext.xml:1.3
--- phpdoc/en/reference/gmp/functions/gmp-gcdext.xml:1.2        Wed Apr 17 02:38:24 
2002
+++ phpdoc/en/reference/gmp/functions/gmp-gcdext.xml    Tue Apr  1 01:39:54 2003
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.2 $ -->
+<!-- $Revision: 1.3 $ -->
 <!-- splitted from ./en/functions/gmp.xml, last change in rev 1.1 -->
   <refentry id="function.gmp-gcdext">
    <refnamediv>
@@ -17,6 +17,45 @@
      Calculates g, s, and t, such that <literal>a*s + b*t = g =
      gcd(a,b)</literal>, where gcd is the greatest common divisor. Returns
      an array with respective elements g, s and t.
+    </para>
+    <para>
+     This function can be used to solve linear Diophantine equations in two
+     variables. These are equations that allow only integer solutions and have the 
form:
+     <informalequation>
+      <alt>a*x + b*y = c</alt>
+      <graphic fileref="images/diophantine-equation.png"/>
+      </informalequation>
+      For more information, go to the <ulink 
url="&url.diophantine-equation;">"Diophantine 
+       Equation" page at MathWorld</ulink>
+    </para>
+    <para>
+     <example>
+      <title>Solving a linear Diophantine equation</title>
+      <programlisting role="php">
+<![CDATA[
+// Solve the equation a*s + b*t = g
+// where a = 12, b = 21, g = gcd(12, 21) = 3
+$a = gmp_init(12);
+$b = gmp_init(21);
+$g = gmp_gcd($a, $b);
+$r = gmp_gcdext($a, $b);
+
+$check_gcd = (gmp_strval($g) == gmp_strval($r['g']));
+$eq_res = gmp_add(gmp_mul($a, $r['s']), gmp_mul($b, $r['t']));
+$check_res = (gmp_strval($g) == gmp_strval($eq_res));
+
+if ($check_gcd && $check_res) {
+    $fmt = "Solution: %d*%d + %d*%d = %d\n";
+    printf($fmt, gmp_strval($a), gmp_strval($r['s']), gmp_strval($b),
+    gmp_strval($r['t']), gmp_strval($r['g']));
+} else {
+    echo "Error while solving the equation\n";
+}
+    
+// output: Solution: 12*2 + 21*-1 = 3
+]]>
+      </programlisting>
+     </example>
     </para>
    </refsect1>
   </refentry>
Index: phpdoc/entities/global.ent
diff -u phpdoc/entities/global.ent:1.86 phpdoc/entities/global.ent:1.87
--- phpdoc/entities/global.ent:1.86     Sun Mar 30 19:53:18 2003
+++ phpdoc/entities/global.ent  Tue Apr  1 01:39:54 2003
@@ -1,6 +1,6 @@
 <!-- -*- SGML -*-
 
- $Id: global.ent,v 1.86 2003/03/31 00:53:18 alindeman Exp $
+ $Id: global.ent,v 1.87 2003/04/01 06:39:54 jmcastagnetto Exp $
 
  Contains global "macros" for all the XML documents.
 
@@ -54,6 +54,7 @@
 <!ENTITY url.dbstyle "http://nwalsh.com/docbook/dsssl/";>
 <!ENTITY url.dbx.docs "http://www.guidance.nl/php/dbx/doc/";>
 <!ENTITY url.dcom.update 
"http://download.microsoft.com/msdownload/dcom/95/x86/en/dcom95.exe";>
+<!ENTITY url.diophantine-equation 
"http://mathworld.wolfram.com/DiophantineEquation.html";>
 <!ENTITY url.dmalloc "http://www.dmalloc.com/";>
 <!ENTITY url.docbook "http://www.oasis-open.org/docbook/";>
 <!ENTITY url.docbook.xml "http://www.nwalsh.com/docbook/xml/";>



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

Reply via email to