2014-09-29 95 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