将对象作为参数传递给方法

时间:2015-05-13 08:37:36

标签: php oop

我需要创建一个将对象传递给方法的PHP方法,因为参数看起来像这样

        UIButton *shareButton=(UIButton*)sender;
        UIActivityViewController *activityVC=[[UIActivityViewController alloc] initWithActivityItems:
       @[@"Programmatically Shared post Just for testing ",
       [ [videosList objectAtIndex:shareButton.tag ]videoUrl]] 
        applicationActivities:nil];

我该如何设计这个方法?

由于

2 个答案:

答案 0 :(得分:1)

Cast他们从arrayobject -

$status = $myClass ->mymethod(
   (object)[
    "server" => "localhost",
    "user" => "user",
    "password" => '123'
   ]
);

方法是 -

public function mymethod($object)
{
    echo $object->server; // or whatever processing you need
}

答案 1 :(得分:0)

您可以将数组类型转换为对象:

<?php
Class myClass {

    public function __construct(){

    }

    public function mymethod($obj){
        return $obj;
    }

}

$status = (new myClass())->mymethod(
    (object)array(
        "server" => "localhost",
        "user" => "user",
        "password" => '123'
    )
);

var_dump($status); 
/* RETURNS:

object(stdClass)#2 (3) {
  ["server"]=>
  string(9) "localhost"
  ["user"]=>
  string(4) "user"
  ["password"]=>
  string(3) "123"
}

*/