WFC Soap class within an class communicating with PHP

Within the Windows Foundation Classes you are able to create SOAP end points. These end points you are able to communicate using classes funny enough (because of the WFC being classes). But to communicate with these is fine with PHP and also when you sometimes have a class within an class as a parameter passing to the SOAP end point. Well you are able to do it within PHP as well.

If you have a WFC service and add these to the service for the ServiceContract and create the DataContract’s and DataMembers

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string NewValue(NewName name);
}
 
[DataContract]
public class SecondName
{
    int svalue1;
    int svalue2;
 
    [DataMember]
    public int Svalue1
    {
        get { return svalue1; }
        set { svalue1 = value; }
    }
 
    [DataMember]
    public int Svalue2
    {
        get { return svalue2; }
        set { svalue2 = value; }
    }
}
 
[DataContract]
public class NewName
{
    SecondName secondValue = new SecondName();
 
    [DataMember]
    public SecondName SecondValue
    {
        get { return secondValue; }
        set { secondValue = value; }
    }
}

Created the SecondName class as the second named class with the svalue1/2 within in turn the NewName named class will reference the SecondName class.

And then within the class that implements the interface here is the function name to call within the soap end point called NewValue.

public string NewValue(NewName namesec)
{
    return string.Format("Value : {0}", namesec.SecondValue.Svalue1 + namesec.SecondValue.Svalue2);
}

Well to find out what you need to pass to the soap call I was using an WFC application to write out the debugging information with altering the web.config by

    <system.serviceModel>
      <diagnostics>
        <messageLogging
             logEntireMessage="true"
             logMalformedMessages="false"
             logMessagesAtServiceLevel="true"
             logMessagesAtTransportLevel="false"
             maxMessagesToLog="3000"
             maxSizeOfMessageToLog="2000"/>
      </diagnostics>
    </system.serviceModel>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="messages"
          type="System.Diagnostics.XmlWriterTraceListener"
          initializeData="c:\temp\messages.svclog" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

which in turns creates a file and in that file there is xml definition what is being sent.

<s:Body>
 <NewValue xmlns="http://tempuri.org/">
 <name xmlns:d4p1="http://schemas.datacontract.org/2004/07/WcfService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 <d4p1:SecondValue><d4p1:Svalue1>200</d4p1:Svalue1><d4p1:Svalue2>100</d4p1:Svalue2></d4p1:SecondValue>
 </name>
 </NewValue></s:Body>
</xml>

and within PHP you are able to copy this. but note that the parameter to send is “name” which is not the same as the parameter above (namesec) you have to send to “name” which is what the soap end point is looking for.

class SecondName {
    public $Svalue1;
    public $Svalue2;
}
 
class NewName {
    public $SecondValue;
 
    public function NewName($s1, $s2)
    {
        $this->SecondValue = new SecondName();
        $this->SecondValue->Svalue1 = $s1;
        $this->SecondValue->Svalue2 = $s2;
    }
}
 
$objN = new NewName(200,100);
 
//Create a SOAP client
$client = new SoapClient("http://192.168.0.3/Service1.svc?wsdl");
$retVal = $client->NewValue(array ("name" => $objN));
 
print_r($retVal->NewValueResult);

and the output is

Value : 300

Have attached a file of the project for the WFC webserver and also the php code.

Final and auto load a class

To include a class within many languages you have to define it at the top and then off you go with programming with using that class. Well within PHP you can use the __autoload function that is called when you try to create a new instance of a class.

function __autoload($class_name) 
{
	include "classes/".$class_name.".php";
}

this __autoload function above takes a parameter as the class name to load and then within the code it will try and load that class with how ever you want to, in this case I have placed the classes within the directory called classes and under there classname.php filename.

So if you try to load the class

$newclass = new basic();

it will look within the classes directory for the file basic.php for the autoload function above, you can alter it to load from where ever and how ever you want to.

The next part of this post is about the keyword ‘final’, the final keyword means that it is final so if you try and extend that class and extend the function that has been declared as final this will cause php interpretor to fail due to syntax error with something similar to

Fatal error: Cannot override final method class_name::function_name()

For an example here is two classes one is called basic and the other is called extendbasic, where the basic class has the final function within it

class basic
{
	public function sayHello()
	{
		echo "<br/>".get_class($this) . " : Hello";
	}
 
	final public function sayGoodbye()
	{
		echo "<br/>".get_class($this) . " : Goodbye";
	}
}

and here is the extendclass

class extendbasic extends basic
{
	public function sayHello()
	{
		echo "<br/>".get_class($this). " : the newer hello!!";	
		parent::sayHello();
	}
        // this will cause the php engine to fail!!!!!. because it is trying to over load the function sayGoogbye which has been declared as final
/*	public function sayGoodbye()
	{
		echo "I should not get here!!";
	}*/
}

Here is some code to call them both which they have been placed within a directory called ‘classes’

function __autoload($class_name) 
{
	include "classes/".$class_name.".php";
}
 
$basicClass = new basic();
$basicClass->sayHello();
$basicClass->sayGoodbye();
$basicClass = new extendbasic();
$basicClass->sayHello();
$basicClass->sayGoodbye();

and the output would be

basic : Hello
basic : Goodbye
extendbasic : the newer hello!!
extendbasic : Hello
extendbasic : Goodbye

I am calling the parent function from within the extendbasic class sayHello function.

Wordsearch php – grid class

From the post on the projects page, wordsearch, here is a basic design of a grid class that can be used to link together with the other aspects of a wordsearch game. This will be able to create a basic grid and populate it with characters.

The reason why I created the createGridRow function was because this class could be inherited and thus alter any aspects of the class to suit the future needs.

The

$this->

within the code references the local variables of the class and not any other variables e.g. local to that function.

The

chr(rand(0,25) + 97);

means create a chr (character) from a random generated value within a range of 0 to 25 (26 letters in the English alphabet) and then add the value 97 to it, because the integer value of 97 – 125 are the ascii codes for a-z characters.

<?php
class Grid {
 
  /* private section of the class grid */
 
  // store the size and the grid
    private $_size = 0;
    private $_grid;
 
 
  /* the public section of the class grid */
 
    // construct the grid with a given size
    public function __construct($size=0) 
    { 
      // set the private variables
      $this->_size = $size; 
      $this->_grid = array();
      for ($i =1; $i <= $this->_size; $i++)
      {
	// create each row of the grid with the class function createGridRow
	$this->_grid[$i] = $this->createGridRow();
      }
    }
 
    // delete the grid 
    public function __destruct()
    {
      // clear up the grid, each line and then the whole object.
      for ($i = 1; $i <= $this->_size; $i++)
      {
	unset($this->_grid[$i]);
      }
      unset($this->_grid);
    }
 
    /* createGridRow, this can be inherited and thus altered to create any type of character in the grid */
    public function createGridRow()
    {
	$_grid_insert = array();
	for ($j = 1; $j <= $this->_size; $j++)
	{
	  $_grid_insert[$j] = chr(rand(0,25) + 97);
	}
	return $_grid_insert;
    }
 
    /* basic print out of a grid */
    public function printOut()
    {
      echo "The grid<br/>";
      for ($i = 1; $i <= $this->_size; $i++)
      {
	echo "$i .. ";
	for ($j = 1; $j <= $this->_size; $j++)
	{
	    echo " $j = ".$this->_grid[$i][$j];
	}
	echo "<br/>";
      }
    }
 
    /* return the size of the grid */
    public function returnSize()
    {
      return $this->_size;
    }
 
    /* insert a character into the grid at position X, Y and the character */
    public function insertCharIntoGrid($pX, $pY, $char)
    {
	$this->_grid[$pY][$pX] = $char;
    }
 
    /* return the grid reference */
    public function returnGrid()
    {
      return $this->_grid;
    }
}
$wordsearch_grid = new Grid(6);
$wordsearch_grid->printOut();
?>

The output of the code as above is

The grid
1 .. 1 = l 2 = l 3 = z 4 = m 5 = h 6 = m
2 .. 1 = u 2 = r 3 = z 4 = s 5 = k 6 = y
3 .. 1 = f 2 = k 3 = j 4 = n 5 = k 6 = p
4 .. 1 = h 2 = p 3 = x 4 = o 5 = c 6 = q
5 .. 1 = s 2 = s 3 = x 4 = r 5 = u 6 = v
6 .. 1 = r 2 = f 3 = g 4 = q 5 = s 6 = o