2014-09-29 24 views
1

我是使用PHP的新手。我正在尝试将Java代码转换为PHP,但收效甚微。尤其是声明long类型的变量类型,并将类的对象声明为数组。将代码从Java转换为PHP格式。

public class Company extends User implements Serializable { 

private static final long serialVersionUID = 8598447488057067508L; 

public String id; 
public String companyName; 
public String companyCode; 
public String avatar; 

public Price price; 
public List<History> history; 
public List<Wall> wallpost; 

public boolean isFollowing; 

public String toString() { 
    return "Company [id=" + id 
      + ", companyName=" + companyName 
      + ", companyCode=" + companyCode 
      + ", avatar=" + avatar + "]"; 
    } 
} 

在PHP

<?php 

include "History.php"; 
include "Price.php"; 
include "Wall.php"; 

class Company extends User implements Serializable { 

    private static final long $serialVersionUID = 8598447488057067508L;// get error here 

    public $id; 
    public $companyName; 
    public $companyCode; 
    public $avatar; 

    public Price $price;//object of Price Class how to declare this? 
    public List<History> $history;//object of History Class how to declare this as a List? 
    public List<Wall> $wallpost;//object of Wall Class how to declare this as a list? 

    public boolean $isFollowing; //how to assign boolean to this variable? 
} 

我相信有shud在PHP铸造这个的一种方式。

感谢您的帮助。

+0

您是否尝试过使用任何工具将java转换为php? – 2015-02-13 00:50:51

+0

@Evan:你能推荐一些免费的开源工具吗? – 2015-07-17 15:50:56

+1

@ MD.MohiuddinAhmed,我试过但没有运气。检查出结果:http://myhashcode.blogspot.sg/2015/02/convert-java-to-php.html – 2015-07-21 02:40:53

回答

1

只是将它们定义为数组,你不能定义一个类型:

<?php 

include "History.php"; 
include "Price.php"; 
include "Wall.php"; 

class Company extends User implements Serializable { 

    // Define as constant, include value in quotes 
    const serialVersionUID = "8598447488057067508L"; 

    public $id; 
    public $companyName; 
    public $companyCode; 
    public $avatar; 

    public $price; 
    public $history = array(); 
    public $wall = array(); 

    public $isFollowing; 

    public function __construct() 
    { 
     // Set price 
     $price_obj = new Price(); 
     array_push($this->price, $price_obj); 

     // Set history 
     $history_obj = new History(); 
     array_push($this->history, $history_obj); 

     // Set wall 
     $wall_obj = new Wall(); 
     array_push($this->wall, $wall_obj); 

     // Assign boolean 
     $this->isFollowing = true; 

    } 
} 

你可以把任何类型的对象在变量/数组。

final关键字只能在class es上使用,但您可以将其定义为常量。

这可能是一个奇怪的概念,从强类型语言到弱类型......

+0

您好coulton,感谢您的答案,但即时通过私人常量$ serialVersionUID =“8598447488057067508L “; “unexpected const”,“unexpected variable”。 – 2014-09-29 10:23:36

+0

对不起,我刚刚在文档http://php.net/manual/en/language.oop5.constants.php注意到这里的顶部注释'这看起来很明显,但类常量总是公开可见的,它们不能被设置为私有的或者是被保护的,我没有在任何地方的文档中看到它的状态。“我已经从常量定义和'$'中删除了'private'形式。我有一个'$' – Luke 2014-09-29 10:26:35

+0

哎呀,我错过了'__construct()'方法中的函数''''''''''''''''' .tehplayground.com /#kBdKRc9TO – Luke 2014-09-29 10:32:25