Using a PHP project as a reference in C##

Please refer to What is NuGet for the introductory article about .NET's packaging system.

This article expands on the creating a NuGet from a PHP project tutorial and describes how to use NuGet packages and PHP libraries seamlessly in C# with respect to the compiled PHP code specifics.

Sample library.nupkg#

The following example demonstrates a PHP library. It contains a single source file declaring a class Class1 within a namespace Library. The class has a single instance method encode taking a single argument of any type (mixed in PHP), returning the value of type string.

library.msbuildproj:

<Project Sdk="Peachpie.NET.Sdk">
  <PropertyGroup>
    <OutputType>library</OutputType>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="**/*.php" />
  </ItemGroup>

</Project>

class1.php:

<?php

namespace Library;

class Class1 {
  // a sample function that encodes given value into a json string:
  function encode($value): string {
      return json_encode($value);
  }
}

The library is compiled and packed into a NuGet package upon building:

dotnet build

Expected command result:

Microsoft (R) Build Engine version 16.4.0 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.        

  Restore completed in 26,77 ms for library.msbuildproj.
  PeachPie PHP Compiler version 1.0.0
  library -> \bin\Debug\netstandard2.0\library.dll
  Successfully created package '\bin\Debug\library.1.0.0.nupkg'.

Build succeeded.

    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.45

Referencing the library#

A. Referencing a <ProjectReference>#

In case the library is part of a .NET solution, it can be referenced as a .NET project.

Open the .csproj file and add the following code snippet. Alter the Include value to point to your library.msbuildproj file.

<ItemGroup>
  <ProjectReference Include="../library/library.msbuildproj" />
</ItemGroup>

B. Referencing a <PackageReference>#

The library can be provided to users as a standard .NET NuGet package. The created .nupkg file is supposed to be published to a NuGet feed. Use the dotnet nuget push command along with an API key. Please see Publishing packages for more details.

Open the .csproj file and add following code snippet:

<ItemGroup>
  <PackageReference Include="library" Version="1.0.0" />
</ItemGroup>

Note: definitely choose a more specific name for your library. A fully namespaced name separated with dots is a common way of naming packages.

Using the library#

Once the library is referenced, either as a ProjectReference or PackgeReference, its public classes and methods become available to the client's C# code.

static void Main(string[] args)
{
    // instantiate PHP class
    var class1 = new Library.Class1();
    // call PHP method with an argument
    string encoded = class1.encode("Hello World!");
    // output the result
    Console.WriteLine(encoded);
}

Remarks#

There are certain conversion conventions between PHP values and C# types. Please see type system and PhpValue for more details on how the values are implicitly converted.

PHP objects and functions are liable to the instance of Context class. If ommited (like in this example) a default instance is provided implicitly. In case of a multithreaded evironment and web applications, make sure you provide an instance of Context corresponding to your current thread.