PHP 数组提取对象

发布于 2024-07-13 18:25:31 字数 667 浏览 8 评论 0原文

假设我有一个用户定义类的对象数组。 想知道如何在 PHP 中提取数组的元素。

// class definition
class User
{
public $fname;
public $lname;
}

// array of objects of the class defined above
$objUser1 = new User():
$objUser2 = new User():
$objUser3 = new User():
$objUser4 = new User():

$alUser = array();
$alUser[] = $objUser1;
$alUser[] = $objUser2;
$alUser[] = $objUser3;
$alUser[] = $objUser4;


// trying to iterate and extract values using typcasting - this does not work, what is the alternative.
foreach($alUser as $user)
{
$obj = (User) $user; // gives error - unexpected $user;
}

这就是我以前在 Java 中从 Java ArrayList 中提取对象时所做的事情,因此认为 PHP 的方式可能类似。 谁能解释一下。

Suppose I have an array of a objects of user defined class. Wanted to know how do I extract the elements of the array in PHP.

// class definition
class User
{
public $fname;
public $lname;
}

// array of objects of the class defined above
$objUser1 = new User():
$objUser2 = new User():
$objUser3 = new User():
$objUser4 = new User():

$alUser = array();
$alUser[] = $objUser1;
$alUser[] = $objUser2;
$alUser[] = $objUser3;
$alUser[] = $objUser4;


// trying to iterate and extract values using typcasting - this does not work, what is the alternative.
foreach($alUser as $user)
{
$obj = (User) $user; // gives error - unexpected $user;
}

Thats how I used to do in java while extracting objects from the Java ArrayList, hence thought the PHP way might be similar. Can anyone explain it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

债姬 2024-07-20 18:25:33

PHP 是一种动态类型语言。 大多数情况下不需要强制转换。

无法转换为用户:请参阅有关 type 的 PHP 文档杂耍和铸造

此示例将打印“$user is a object (User)”四次。

foreach($alUser as $user) {
    echo '$user is a ' . get_type($user);

    if(is_object($user)) {
        echo ' (' . get_class($user) . ')';

    echo "\n";
}

PHP is a dynamically typed language. There is no need to cast in most cases.

It is impossible to cast to a User: see PHP's documentation on type juggling and casting.

This example would print "$user is a object (User)" four times.

foreach($alUser as $user) {
    echo '$user is a ' . get_type($user);

    if(is_object($user)) {
        echo ' (' . get_class($user) . ')';

    echo "\n";
}
不忘初心 2024-07-20 18:25:33

例如,Eclipse PDT 最好能够确定代码完成的对象类型。 否则,您将陷入回溯,在哪里创建数组以及将哪些对象放入其中,然后查看类文件以查看哪些函数可用(或者临时创建一个新的 theObject() 以查看可用的方法/属性,如果您如果许多对象调用创建这些数组和其中的对象的函数,那么知道它是什么类型的对象有时可能并不那么容易,因此必须回溯以查看这些数组是如何创建的)。 听说其他一些 IDE 可能能够像 phpEd 一样更好地确定类型?

It would be nice for example Eclipse PDT to determine the type of object for Code Completion. otherwise you are stuck backtracing, where the array was created and what objects were put into it and then look at the class file to see what functions are available (or temp create a new theObject() to see what methods/properties are available if you know what type of object it is. other times may not be as easy if many objects call functions that create those arrays and objects in them, so have to backtrace to see how those arrays made). Heard a few other IDE's may be able to determine type better like phpEd possibly?

柒七 2024-07-20 18:25:32
foreach ($alUser as $user) {
    $obj = $user;
}

为什么需要为此进行类型转换?

foreach ($alUser as $user) {
    $obj = $user;
}

Why do you need typecasting for this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文