草庐IT

Construct

全部标签

PHP PDO : Fetching data as objects - properties assigned BEFORE __construct is called. 这是正确的吗?

完整的问题应该是“这是正确的还是我不能指望的错误?”WHYisthiscorrectbehavior?我一直在使用PDO,尤其是直接将数据提取到对象中。在这样做的过程中,我发现了这一点:如果我像这样直接将数据提取到对象中:$STH=$DBH->prepare('SELECTfirst_name,addressfrompeopleWHERE1');$obj=$STH->fetchAll(PDO::FETCH_CLASS,'person');并有一个像这样的对象:classperson{public$first_name;public$address;function__construct

c++ - allocator_traits::construct() 与 allocator_traits::allocate()

C++11提供了std::allocator_traits类作为使用分配器的标准方式。静态函数std::allocator_traits::construct()将一个指针指向应该构造对象的位置。然而,std::allocator_traits::allocate()静态函数返回一个allocator::pointer值,它只需要表现得像一个指针,但不一定一个(一般来说,虽然std::allocator::pointer需要是一个指针)。如果分配和构造静态方法通常会与不兼容的类型一起工作,那么应该如何使用它们?只有当pointer类型实际上可以转换为普通指针时才能使用它们吗?

php - 最佳实践,覆盖 __construct() 与提供 init() 方法

当您对对象进行子类化并希望扩展初始化代码时,有两种方法。覆盖__construct(),并实现父类(superclass)构造函数调用的初始化方法。方法一:classfoo{publicfunction__construct($arg1,$arg2,$arg3){//Doinitialization}}classbarextendsfoo{publicfunction__construct($arg1,$arg2,$arg3){parent::__construct($arg1,$arg2,$arg3);//Dosubclassinitialization}}方法二classfoo{p

Java XML : using DOM with StAX to construct a document

我正在使用StAX使用XMLStreamWriter构建XML文档.但是,我的文档的某些部分很难逐个调用XMLStreamWriter的方法,使用DOM构建一个小的文档片段,然后将其写出来会更容易。我知道如何使用DOM,但这是我的问题:有没有简单的方法来获取Element对象并将其写出到XMLStreamWriter?我可能会想出如何“连接”这两种方法,但它似乎很乏味而且应该已经有一些东西了。(走另一条路似乎微不足道:http://blogs.oracle.com/venu/entry/constructing_dom_using_stax_writers)

c++ - std::allocator_traits::construct with const 指针

下面的代码可以正常编译:#include#includeintmain(){constint*a=newint(5);std::cout>;autoalloc=std::allocator();at::construct(alloc,a);std::cout在libstdc++的背后::new((void*)a)int;但是a是const!这是未定义的行为吗?或者placementnew不算修改?我修改了*a的值,是const。据我了解,这是不允许的:Modifyingaconstobjectthroughanon-constaccesspathandreferringtoavolat

php - 如何在 __construct 中使用其父工厂实例化子类

我有一个必须使用其他倍数对象实例化的A类classA{function__construct(newX(),newY(),newZ()){$this->foo='foo';}}为了省去这个类实例化的麻烦,我为这个类建立了一个工厂。classA_Factory{publicstaticfunctioncreate_A(){returnnewA(newX(),newY(),newZ());}}我有一个扩展类A的类B。我的问题是我不知道如何在B类中实例化A类以访问属性“foo”。我觉得尝试很自然:classBextendsA{function__construct(){A_Factory::

扩展类 __construct 上的 PHP OOP 更新 protected 字符串

我正在尝试创建我的第一个PHP类,但一直被困在如何更新protected字符串上。我想做的是创建一个扩展类,该扩展类可以处理来自主类的protected字符串。我能够在第一个类加载时更新字符串,但是当我加载我的扩展类时它不显示更新的文本。我做错了什么?classtest{protected$testing='test';function__construct(){echo"TheTestclasshasloaded(".$this->testing.")";$this->testing='changed';echo"Updatedto(".$this->testing.")";}}cl

php - 扩展 PDO 的类 - parent::__construct 不起作用,但创建新的 PDO 可以

我正在尝试编写一个PDO包装器,但我在构造函数方面遇到了一些问题。理想情况下,我想调用父级的构造函数,但由于某种原因,这是行不通的。我尝试(测试)检查是否创建了一个新的PDO并且确实有效,我发现这最令人困惑。这是我的代码:classdbextendsPDO{private$dbconn;publicfunction__construct(){$dsn='mysql:dbname='.MYSQL_DB.';host='.MYSQL_HOST;$user=MYSQL_USER;$pw=MYSQL_PW;try{$this->dbconn=parent::__construct($dsn,$

PHP Mongo:注意:Mongo::__construct(): 解析服务器

我想知道为什么我会收到以下php通知:(!)Notice:Mongo::__construct():parsingserversinC:\htdocs\multishop\library\Lupi\Resource\Odm.phponline38CallStack#TimeMemoryFunctionLocation10.0004138504{main}()..\index.php:020.0130667392Zend_Application->bootstrap()..\index.php:2530.0130667488Zend_Application_Bootstrap_Boots

php - self::__construct() 和 new self() 之间的确切区别

你能告诉我returnself::__construct()和returnnewself()之间的确切区别吗?看起来实际上可以在创建对象时从__construct()调用返回一个self::__construct(),返回对象本身就像第一个__construct()从未被调用过。 最佳答案 这在代码中得到了最好的说明:classMyClass{public$arg;publicfunction__construct($arg=NULL){if($arg!==NULL)$this->arg=$arg;return$this->arg;