PHP100 2019-03-27
代码如下:
$db = Zend_Db::factory('Pdo_Mysql', array( 'host' => '127.0.0.1', 'username' => 'webuser', 'password' => 'xxxxxxxx', 'dbname' => 'test' ));
代码如下:
$db->setFetchMode(Zend_Db::FETCH_OBJ); $result = $db->fetchAssoc( 'SELECT bug_id, bug_description, bug_status FROM bugs' );
代码如下:
< ?php class Zend_Db { /** 设定一些常量和默认值 */ /** * Factory for Zend_Db_Adapter_Abstract classes. * * First argument may be a string containing the base of the adapter class * name, e.g. 'Mysqli' corresponds to class Zend_Db_Adapter_Mysqli. This * name is currently case-insensitive, but is not ideal to rely on this behavior. * If your class is named 'My_Company_Pdo_Mysql', where 'My_Company' is the namespace * and 'Pdo_Mysql' is the adapter name, it is best to use the name exactly as it * is defined in the class. This will ensure proper use of the factory API. * * First argument may alternatively be an object of type Zend_Config. * The adapter class base name is read from the 'adapter' property. * The adapter config parameters are read from the 'params' property. * * Second argument is optional and may be an associative array of key-value * pairs. This is used as the argument to the adapter constructor. * * If the first argument is of type Zend_Config, it is assumed to contain * all parameters, and the second argument is ignored. * * @param mixed $adapter String name of base adapter class, or Zend_Config object. * @param mixed $config OPTIONAL; an array or Zend_Config object with adapter parameters. * @return Zend_Db_Adapter_Abstract * @throws Zend_Db_Exception */ public static function factory ($adapter, $config = array()) { //使用Zend_Config对象,上述方式没有使用,直接使用Array if ($config instanceof Zend_Config) { $config = $config->toArray(); } /* * Convert Zend_Config argument to plain string * adapter name and separate config object. */ if ($adapter instanceof Zend_Config) { if (isset($adapter->params)) { $config = $adapter->params->toArray(); } if (isset($adapter->adapter)) { $adapter = (string) $adapter->adapter; } else { $adapter = null; } } /* * Verify that adapter parameters are in an array. */ if (! is_array($config)) { /** * @see Zend_Db_Exception */ require_once 'Zend/Db/Exception.php'; throw new Zend_Db_Exception( 'Adapter parameters must be in an array or a Zend_Config object'); } /* * Verify that an adapter name has been specified. */ if (! is_string($adapter) || empty($adapter)) { /** * @see Zend_Db_Exception */ require_once 'Zend/Db/Exception.php'; throw new Zend_Db_Exception( 'Adapter name must be specified in a string'); } /* * Form full adapter class name */ $adapterNamespace = 'Zend_Db_Adapter'; if (isset($config['adapterNamespace'])) { if ($config['adapterNamespace'] != '') { $adapterNamespace = $config['adapterNamespace']; } unset($config['adapterNamespace']); } // Adapter no longer normalized- see http://framework.zend.com/issues/browse/ZF-5606 $adapterName = $adapterNamespace . '_'; $adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter)))); /* * Load the adapter class. This throws an exception * if the specified class cannot be loaded. */ if (! class_exists($adapterName)) { require_once 'Zend/Loader.php'; Zend_Loader::loadClass($adapterName); } /* * Create an instance of the adapter class. * Pass the config to the adapter class constructor. */ $dbAdapter = new $adapterName($config); /* * Verify that the object created is a descendent of the abstract adapter type. */ if (! $dbAdapter instanceof Zend_Db_Adapter_Abstract) { /** * @see Zend_Db_Exception */ require_once 'Zend/Db/Exception.php'; throw new Zend_Db_Exception( "Adapter class '$adapterName' does not extend Zend_Db_Adapter_Abstract"); } return $dbAdapter; } }
代码如下:
//factory有一个参数叫做$adapter public static function factory($adapter, $config = array()) //确定namespace $adapterNamespace = 'Zend_Db_Adapter'; //用namespace和上面传入的$adapter构造类名 $adapterName = $adapterNamespace . '_'; $adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter)))); //用上面生成的类名new出obj,看起来PHP比java方便那么一点点哈(Class.forName(‘XXX').newInstance()) $dbAdapter = new $adapterName($config);
代码如下:
$db = Zend_Db::factory('Pdo_Mysql', array( 'host' => '127.0.0.1', 'username' => 'webuser', 'password' => 'xxxxxxxx', 'dbname' => 'test' ));