[libvirt] [libvirt-php][PATCH 09/14] examples: Properly detect if we're connected

Michal Privoznik mprivozn at redhat.com
Fri Jul 8 13:23:18 UTC 2016


So we have this special class Libvirt implemented in
examples/libvirt.php. It's aim is to wrap some low-level libvirt
APIs into slightly more advanced methods. Now, when instantiating
the class, connection URI can be passed in which case the class
will connect to it right in the constructor. However, later in
the code, the returned value (which is an object reference) is
compared against false, which will never ever be true.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 examples/index.php   | 4 ++--
 examples/libvirt.php | 5 ++---
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/examples/index.php b/examples/index.php
index 687f408..dbaa1bf 100644
--- a/examples/index.php
+++ b/examples/index.php
@@ -1,7 +1,7 @@
 <?php
     require('libvirt.php');
-    $lv = new Libvirt('qemu:///system');
-    if ($lv == false)
+    $lv = new Libvirt();
+    if ($lv->connect("qemu:///system") == false)
         die('<html><body>Cannot open connection to hypervisor</body></html>');
     $hn = $lv->get_hostname();
     if ($hn == false)
diff --git a/examples/libvirt.php b/examples/libvirt.php
index c60889a..14436d8 100644
--- a/examples/libvirt.php
+++ b/examples/libvirt.php
@@ -5,11 +5,9 @@ class Libvirt {
     private $allow_cached = true;
     private $dominfos = array();
 
-    function Libvirt($uri = false, $debug=false) {
+    function Libvirt($debug = false) {
         if ($debug)
             $this->set_logfile($debug);
-        if ($uri != false)
-            $this->connect($uri);
     }
 
     function _set_last_error() {
@@ -32,6 +30,7 @@ class Libvirt {
         $this->conn=libvirt_connect($uri, false);
         if ($this->conn==false)
             return $this->_set_last_error();
+        return true;
     }
 
     function domain_disk_add($domain, $img, $dev, $type='scsi', $driver='raw') {
-- 
2.8.4




More information about the libvir-list mailing list