Object 对象

对象初始化

要创建一个新的对象 object,使用 new 语句实例化一个类: 

<?php

class foo

{

    function do_foo()

    {

        echo "Doing foo."; 

    }

}

$bar = new foo;

$bar->do_foo();

?>  

详细讨论参见手册中类与对象章节。 

转换为对象

如果将一个对象转换成对象,它将不会有任何变化。如果其它任何类型的值被转换成对象,将会创建一个内置类 stdClass 的实例。如果该值为 NULL,则新的实例为空。数组转换成对象将使键名成为属性名并具有相对应的值。对于任何其它的值,名为 scalar 的成员变量将包含该值。 

<?php

$obj = (object) 'ciao';

echo $obj->scalar;  // outputs 'ciao'

?>  

User Contributed Notes

qeremy [atta] gmail [dotta] com 02-Mar-2012 09:41 

 Do you remember some JavaScript implementations?

 // var timestamp = (new Date).getTime();

 Now it's possible with PHP 5.4.*;

<?php

class Foo

{

     public $a = "I'm a!";

     public $b = "I'm b!";

     public $c;

     public function getB() {

         return $this->b;

     }

     public function setC($c) {

         $this->c = $c;

         return $this;

     }

     public function getC() {

         return $this->c;

     }

 }

 print (new Foo)->a;      // I'm a!

print (new Foo)->getB(); // I'm b!

?>

 or

<?php

// $_GET["c"] = "I'm c!";

print (new Foo)

        ->setC($_GET["c"])

        ->getC(); // I'm c!

?>   

helpful at stranger dot com 04-Jan-2012 10:15 

 By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose: 

<?php $genericObject = new stdClass(); ?> 

 I had the most difficult time finding this, hopefully it will help someone else!  

razvan_zarzu at yahoo dot com 19-Oct-2011 04:46 

 In PHP 5.3.6, even if the following code doesn't have any apparent syntax errors:

<?php

class Test {

   public $a = SplFixedArray::fromArray(array());

 }

?>

 , the result is:

 Parse error: syntax error, unexpected '(', expecting ',' or ';' in ~\test.php on line 3

 This is because on an implicit object initialization of $a, which is not allowed in PHP.

 Hope this helps someone.  

Cosmitar: mhherrera31 at hotmail 08-Dec-2010 07:47 

 i would like to share a curious behavior on casted objects. Casting an object from a class with private/protected attributes results a stdClass with a private/protected attribute for get.

 Example:

<?PHP

class Foo{

  private $priv = 1;

  public $pub = 2;

  public function getSimple(){

   return (object)(array) $this; //the array cast is to force a stdClass result

  }

 }

$bar = new Foo();

var_dump($bar->getSimple();// output: object(stdClass)#3 (2) { ["priv:private"]=> int(1) ["pub"]=> int(2) }

var_dump($bar->getSimple()->priv);// output: NULL, not a Fatal Error

var_dump($bar->getSimple()->pub);// output: int(2)

$barSimple = $bar->getSimple();

$barSimple->priv = 10;

var_dump($barSimple->priv);// output: int(10)

?>   

Anonymous 05-Jan-2010 11:10 

 Initialization, Instantiation and Instances are terms that can be confusing at first. Let's try to sort it out starting with this simple class definition.

<?php

class User

{

   public $first_name;

   public $last_name;

   public function __toString()

   {

     return "User [first='$this->first_name', last='$this->last_name']";

   }

 }

?>

 Now create several INSTANCES of User by INSTANTIATING the User class above.

<?php

 $user_1 = new User;        // $user_1 is an INSTANCE of User

$user_2 = new User;        // $user_2 is an INSTANCE of User

echo $user_1 . '<br>';     // User [first='', last='']

echo $user_2 . '<br>';     // User [first='', last='']

?>

 Here we have (2) two INSTANCES of User, but each instance was only INSTANTIATED once - when we used the 'new' operator.

 And now looking at the printed output, you can see there are no values for their first or last names. This means that the objects themselves have NOT been INITIALIZED. To remedy this situation, rewrite the class definition by adding a __construct() method.

<?php

class User

{

   public $first_name;

   public $last_name;

   public function __construct($first, $last)    // Require first and last names when INSTANTIATING

   {

     $this->first_name = $first;                 // INITIALIZE $first_name;

     $this->last_name = $last;                   // INITIALIZE $last_name;

   }

   public function __toString()

   {

     return "User [first='$this->first_name', last='$this->last_name']";

   }

 }

?>

 Now try it again.

<?php

 $user_1 = new User('John', 'Doe');      // $user_i is an INSTANCE of User  

$user_2 = new User('Jane', 'Doe');      // $user_2 is an INSTANCE of User

echo $user_1 . '<br>';                  // prints: User [first='John', last='Doe']

echo $user_2 . '<br>';                  // prints: User [first='Jane', last='Doe']

?>

 The __construct() method is called automatically by PHP when it sees the 'new' operator. Our __construct() method above requires the first and last names to be passed in as arguments and uses them to INITIALIZE objects when INSTANTIATING new INSTANCES.  

mailto dot aurelian at gmail dot com 18-Dec-2009 06:56 

 You can create [recursive] objects with something like:

<?php

   $literalObjectDeclared = (object) array(

      'foo' => (object) array(

           'bar' => 'baz',

           'pax' => 'vax'

       ),

       'moo' => 'ui'

    );

 print $literalObjectDeclared->foo->bar; // outputs "baz"!

?>   

spidgorny at gmail dot com 22-Jul-2009 10:58 

 If having

 class BugDetails extends Bug

 you want to cast an object of a Bug to BugDetails like this

<?php

     $clone = (BugDetails) clone $this;

// OR

     $clone = (BugDetails) $bug;

?>

which doesn't work in PHP, you have two options:

 1. Copying all (including private) properties manually (you could also use get_object_vars(), but this is shorter):

<?php

     $clone = new BugDetails();

     foreach ($this as $key => $val) {

         $clone->$key = $val;

     }

?>

2. Serialize an object Bug, manipulate the resulting string so that it has BugDetails inside and unserialize it.

 See here: http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/

 Spent two hours looking for more elegant solution, that's my findings.  

cFreed at orange dot fr 23-Apr-2009 03:21 

 CAUTION:

 "Arrays convert to an object with properties named by keys, and corresponding values".

 This is ALWAYS true, which means that even numeric keys are accepted when converting.

 But the resulting properties cannot be accessed, since they don't match the variables naming rules.

 So this:

<?php

 $x = (object) array('a'=>'A', 'b'=>'B', 'C');

 echo '<pre>'.print_r($x, true).'</pre>';

?>

works and displays:

 stdClass Object

 (

     [a] => A

     [b] => B

     [0] => C

 )

 But this:

<?php

echo '<br />'.$x->a;

 echo '<br />'.$x->b;

 echo '<br />'.$x->{0}; # (don't use $x->0, which is obviously a syntax error)

?>

fails and displays:

 A

 B

 Notice: Undefined property: stdClass::$0 in...  

wbcarts at juno dot com 30-Sep-2008 12:40 

 ON-THE-FLY!!!

 In response to Bobs note below (and Rick's somewhat): It is much better to predefine the object you want to create. This is because in all the programs you write, you will still create objects On-the-Fly anyway:

 $foo = 'My String Object';      // on-the-fly!

 $bar = 45;                      // on-the-fly!

 $obj = new stdClass();          // on-the-fly!

 $obj->blah = 3;                 // on-the-fly!

 So what's missing above? Real control over WHAT your program does, HOW it does it, and HOW your data is organized. Let's say your code $obj->blah = 3 was 'blah' because 3 was your last Test Score (take no offense, I've done worse!). In that case, you could use code like so:

<?php

class Test{

   const MIN = 0, MAX = 100;     // acceptable range for a test

   protected $person;            // person taking the test

   protected $score;             // use the word 'score' because 'blah' is meaningless

   public function __construct($p, $s){

     $this->person = $p;

     $this->score = self::clamp($s);

   }

   protected static function clamp($val){

     if($val < self::MIN) $val = self::MIN;

     if($val > self::MAX) $val = self::MAX;

     return $val;

   }

   public function __toString(){

     return "Test [person=$this->person, score=$this->score%]";

   }

 }

$obj = new Test("John Doe", 83);    // on-the-fly!

echo (object)$obj;                  // outputs 'Test [person=John Doe, score=83%]'

?>

 Writing your own class definitions help maintain the "integerity" of your objects - see how the clamp() function keeps test scores between 0 and 100 percent? PHP  uses similar techniques to set min and max vals on Numbers. And, classes like Test are way more useful than objects created with an empty shell, uh em, stdClass. In addition, you can see WHAT, WHERE, WHY, and HOW your program actually works... On-the-Fly!

 -->  

Isaac Z. Schlueter i at foohack dot com 31-Aug-2008 02:31 

 In response to Harmor and Mithras,  you can use the json functions to convert multi-dimensional arrays to objects very reliably.

 Also, note that just using (object)$x doesn't allow you to access properties inline.  For example, this is invalid:

<?php

 $x = array("foo"=>"bar");

 echo ((object)$x)->foo; // PHP Parse error, unexpected T_OBJECT_OPERATOR

?>

 However, this function will let you do that, and will also handle multi-dimensional arrays without any hassle.

<?php

function to_object ($x) {

     return (is_object($x) || is_array($x)) ? json_decode(json_encode($x)) : (object) $x;

 }

 echo to_object( array("foo"=>"bar") )->foo; // "bar"

?>

 Note that *numeric* arrays will not be converted to objects using this method.  

Mithras 21-Aug-2008 03:54 

 In response to harmor: if an array contains another array as a value, you can recursively convert all arrays with:

<?php

function arrayToObject( $array ){

   foreach( $array as $key => $value ){

     if( is_array( $value ) ) $array[ $key ] = arrayToObject( $value );

   }

   return (object) $array;

 }

?>   

Rick 18-Apr-2008 07:35 

 Bob, I think you solution is to use stdClass and add properties on the fly...

 $obj = new stdClass();

 $obj->data ="This is the solution";  

bob 10-Apr-2008 05:03 

 Why is there nothing in the docs about instantiating a generic object?  ie new object() - it does not work.

 If one wants to create a dynamic object on the fly, the only option I see is to create it implicitly, by creating a child of it - ghetto:

 unset($obj);

 $obj->blah = 3;  

gabe at fijiwebdesign dot com 22-May-2007 06:25 

 In response to sirbinam.

 You cannot call a function or method before it exists. In your example, the global instance of stdout is just being passed around to differnet references (pointers). It however exists in the "dump" function scope via the global keyword. 

 The code below works fine and illustrates that "stdout" has been defined before its instantiation. 

<?php

class profiler{

   function profiler(){

     $this->starttime = microtime();

   }

   function dump(){

     global $stdout;

     $this->endtime = microtime();

     $duration = $this->endtime - $this->starttime;

     $stdout->write($duration);

   }

 }

 class stdout{

   function write($msg){

     echo $msg;

   }

 }

$stdout =& new stdout();

$profiler =& new profiler();

$profiler->dump();

?>

 All classes and functions declarations within a scope exist even before the php execution reaches them. It does not matter if you have your classes defined on the first or last line, as long as they are in the same scope as where they are called and are not in a conditional statement that has not been evaluated yet.  

ludvig dot ericson at gmail dot com 19-Aug-2006 12:35 

 In reply to the usort thing, you can access a property of an object dynamically by:

<?php

 $obj = (object)array("Test" => "bar")

$var = "Test";

 echo $obj->$var;

?>

This will output "bar", and do notice I call on ->$var and not just ->var.  

Trevor Blackbird > yurab.com 27-Nov-2005 06:33 

 You can create a new object using the built-in stdClass or by using type-casting:

<?php

// This is the proper way

$object1 = new stdClass();

// This works too

$object2 = (object) NULL;

// This will create an object from an array

$monkey_array = array('title'=>'Spider Monkey', 'src'=>'monkey.jpg');

$monkey_object = (object) $monkey_array;

 print $monkey_object->title . ' ' . $monkey_object->src;

// You can type-cast in the middle of an expression

function customHTML($some_object) {

// this function expects an object as the argument and returns some output

}

 print '<p>Writing some output ' . customHTML( (object) array('rows'=>3, 'cols'=>4) );

?>   

iblun at gmx dot net 09-Mar-2005 02:08 

 To sort an array, that contains an object, after one fieldname inside the object, im using this function:

 function objectSort($objectarray, $field)

 {

     for ($a=0;$a < (count($objectarray)); $a++)

     {

         for ($b=0;$b < (count($objectarray)); $b++)

         {    

             if ($objectarray[$a]->$field < $objectarray[$b]->$field)

             {

                 $temp = $objectarray[$a];

                 $objectarray[$a] = $objectarray[$b];

                 $objectarray[$b] = $temp;

             }

         }

     }

     return $objectarray;

 }  

mortoray at ecircle-ag dot com 16-Feb-2005 11:07 

 If you use new to create items in an array, you may not get the results you want since the parameters to array will be copies of the original and not references.

 By Example:

 class Store {

     var $item = 3;

 }

     $a = array( new Store() );

     $b = $a;

     $a[0]->item = 2;

     print( "|" . $b[0]->item . "| <br>" );   //shows 3

     $a = array();

     $a[] =& new Store();

     $b = $a;

     $a[0]->item = 2;

     print( "|" . $b[0]->item . "| <br>" );   //shows 2

 This is extremely important if you intend on passing arrays of classes to functions and expect them to always use the same object instance!

 Note: The following syntax is desired (or maybe even the default notation should translate as this):

    $a = array( &new Store() );  

nconantj 18-Jul-2004 09:43 

 php at electricsurfer.com,

 More than a year later and here's some clarification of what's happening in your code, via comments in an otherwise verbatim copy.

 <?

 class c

 {

    var $a = array('a'=>'aa','b'=>'ab');

    var $b = 'c';

    function show()

    {

        echo $this->a['a']; // -> 1st

        echo $this->a['b']; // outputs 'ab'

        $a = 'a';

        $b = 'b';

        echo $this->$a[$a]; // [] 1st, not what I expected

        //Above first becomes $this->$a['a'] by looking at the function's local $a

        //Next it becomes $this->a by again looking at the function's local $a, which references the class variable $a with no subscripts.

        // In order to reference elements of the class variable $a, 

        // you want to use $this->a[$a]

        echo $this->$a[$b]; // does NOT output 'ab'

        // Same as above, but the first step $b becomes 'b'

        $this_a =& $this->$a; // work-around

        echo $this_a[$a]; // no question

        echo $this_a[$b];

        $a_arr = array('a'=>'b');

        echo $this->$a_arr[$a]; // [] 1st => outputs 'c'

        // This becomes $this->$a_arr['a'] which becomes $this->c,

        // by referencing the local variables first.

    }

 }

 $c = new c();

 $c->show();

 ?>  

info at keltoi-web dot com 25-Aug-2003 11:26 

 PHP supports recursive type definitions as far as I've tried. The class below (a _very_ simple tree) is an example:

 class Tree {

 var $_value = null;

 var $_children = array();

 function Tree ($value) {

   $this->_value = $value;

 }

 function addChild ($value) {

   $aux_node = new Tree ($value);

   $this->_children [] = $aux_node;

   return $aux_node; 

 }

 }

 As you can see, in addChild we reference Tree again...

 However, you must be careful about references. See the chapter "References explained" for more details.

 Hope this helps.  

Array 数组

Resource 资源类型

类型

PHP Manual

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

热门产品

php编程基础教程.pptx|php编程培训,php,编程,基础,教程,pptx
php编程基础教程.pptx

历史上的今天:04月29日

热门专题

卓越综合高中|卓越综合高中
卓越综合高中
易捷尔高职单招|易捷尔高职单招,易捷尔高职单招培训,单招分数线,单招录取分数线,高职单招学校分数线
易捷尔高职单招
天麻的功效与作用吃法|天麻的功效与作用,天麻的功效与作用吃法,天麻炖什么治头痛最好,天麻的功效与作用禁忌,天麻多少钱一斤,天麻的功效与作用吃法及禁忌,天麻怎么吃效果最好,天麻粉的功效与作用,天麻怎么吃
天麻的功效与作用吃法
安徽中源管业|安徽中源管业,安徽中源管业mpp电力管,安徽中源管业cpvc电力管,安徽中源管业pe穿线管,安徽中源管业电力管,安徽中源管业排水管,安徽中源管业通信管,安徽中源管业管材
安徽中源管业
大理科技管理学校|大理科技管理学校,大理科技,大理科技中等职业技术学校,大理科技管理中等职业技术学校,大理科技学校
大理科技管理学校
安徽开放大学|安徽开放大学报名,安徽开放大学报考,安徽开放大学,什么是安徽开放大学,安徽开放大学学历,安徽开放大学学费,安徽开放大学报名条件,安徽开放大学报名时间,安徽开放大学学历,安徽开放大学专业
安徽开放大学
开放大学|开放大学报名,开放大学报考,开放大学,什么是开放大学,开放大学学历,开放大学学费,开放大学报名条件,开放大学报名时间,开放大学学历,开放大学专业
开放大学
云南开放大学|云南开放大学报名,云南开放大学报考,云南开放大学,什么是云南开放大学,云南开放大学学历,云南开放大学学费,云南开放大学报名条件,云南开放大学报名时间,云南开放大学学历,云南开放大学专业
云南开放大学

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部