simple map class doesn't fail like it does when deployed - php

I am working on some old php code. in dev envioronment and error_reporting turned on to E_ALL | E_STRICT i get Notice: Undefined offset: 4 in /home/pgnetdev/www/PGNet/php-includes/base_classes.inc on line 545
the offending php class is
<?php
class CMap
{
var $m_aData;
var $m_aKeys;
var $m_iNumItems;
var $m_iCurrentDatumIndex;
function CMap()
{
$this->m_aData = array();
$this->m_aKeys = array();
$this->m_iNumItems = 0;
$this->m_iCurrentDatumIndex = 0;
}
function Add($strKey, & $clDatum)
{
if ($this->Contains($strKey))
return false;
$this->m_aKeys[$strKey] = $strKey;
$this->m_iNumItems++;
$this->m_aData[$strKey] = & $clDatum;
return true;
}
function &First()
{
if ($this->m_iNumItems <= 0)
return null;
sort($this->m_aKeys);
$this->m_iCurrentDatumIndex = 0;
return $this->m_aData[$this->m_aKeys[0]];
}
//line 545 in this method
function &Next()
{
fwrite(STDERR, "+[NEXT]: {$this->m_iCurrentDatumIndex} >= {$this->m_iNumItems}\n"); //not deployed
if ($this->m_iCurrentDatumIndex >= $this->m_iNumItems)
{
fwrite(STDERR, "-[NEXT]: returning null\n");// not deployed
return null;
}
fwrite(STDERR, "-[NEXT]: returning value\n"); //not deployed
return $this->m_aData[$this->m_aKeys[++$this->m_iCurrentDatumIndex]];// line 545
}
function &Find($strKey)
{
if (array_key_exists($strKey, $this->m_aData))
return $this->m_aData[$strKey];
return null;
}
function Contains($strKey)
{
return array_key_exists($strKey, $this->m_aData);
}
function Items()
{
return $this->m_iNumItems;
}
}
my tests for it are
<?php
error_reporting(E_ALL | E_STRICT);
require_once '../PGNet/php-includes/base_classes.inc';
class CMapTest extends PHPUnit_Framework_TestCase
{
public function testItemsReturnsNumberOfItemsAdded()
{
$map = new CMap();
$var = 1;
$this->assertEquals(0, $map->Items(), 'No Items, Zero Count expected');
$map->Add("KEY", $var);
$this->assertEquals(1, $map->Items(), 'Item just added, should have count 1');
}
public function testContainsReturnsTrueIfMapContainsKey()
{
$map = new CMap();
$var = 1;
$this->assertFalse($map->Contains("KEY"), 'No Items, No KEYS');
$map->Add("KEY", $var);
$this->assertTrue($map->Contains("KEY"), 'Item just added, should a key');
}
public function testFindReturnsNullIfMapDoesntContainsKey()
{
$map = new CMap();
$var = 1;
$map->Add("KEY", $var);
$actual = $map->Find("NO_KEY");
$this->assertEquals(null, $actual, 'no key no value... NULL is expected here');
}
public function testFindReturnsValueIfMapContainsKey()
{
$map = new CMap();
$var = 1;
$map->Add("KEY", $var);
$actual = $map->Find("KEY");
$this->assertEquals($var, $actual, 'has key finds value... var is expected here');
}
public function testAddReturnsFalseIfKeyIsAlreadyAdded()
{
$map = new CMap();
$var1 = 1;
$var2 = 2;
$willBeTrue = $map->Add("KEY", $var1);
$willBeFalse = $map->Add("KEY", $var2);
$this->assertTrue($willBeTrue, "first key added should always be true");
$this->assertFalse($willBeFalse, "key already exists so you cant replace it");
}
public function testFirstReturnsNullWhenNoItemsAdded()
{
$map = new CMap();
$actual = $map->First();
$this->assertEquals(null, $actual, 'no items added, returns false');
}
public function testFirstReturnsFirstSortedValue()
{
$map = new CMap();
$var1 = 1;
$var2 = 2;
$map->Add("B", $var2);
$map->Add("A", $var1);
$actual = $map->First();
$this->assertEquals($var1, $actual, 'keys are sorted then first one is grabbed');
}
public function testNextReturnsNullIfOnlySingleItemAdded()
{
$map = new CMap();
$var1 = 1;
$map->Add("A", $var1);
$actual = $map->First();
$this->assertEquals($var1, $actual, 'keys are sorted then first one is grabbed');
$this->assertEquals(null, $map->Next());
}
public function testNextReturnsNullAfterAllItemsAreIteratedThrough()
{
fwrite(STDERR, "+[testNextReturnsNullAfterAllItemsAreIteratedThrough]\n");
$map = new CMap();
$var1 = 1;
$var2 = 2;
$map->Add("B", $var2);
$map->Add("A", $var1);
$actual = $map->First();
$loopCount = 0;
try
{
for($actual = $map->First(); $actual != null; $actual = $map->Next())
{
$this->assertNotNull($actual);
$loopCount++;
}
}
catch (Throwable $th)
{
fwrite(STDERR, "-[testNextReturnsNullAfterAllItemsAreIteratedThrough]:WTH\n");
$this->assertFalse(true, $th->getMessage());
}
$this->assertEquals(2, $loopCount, "the for loop should work");
fwrite(STDERR, "-[testNextReturnsNullAfterAllItemsAreIteratedThrough]\n");
}
}
The last test in here is what I was really puzzled why it isn't breaking or throwing or anything. the console output for that is
$ phpunit ../PGNetTests/tests/unitTests/
PHPUnit #package_version# by Sebastian Bergmann.
.......................................................+[NEXT]: 0 >= 1
-[NEXT]: returning value
.+[testNextReturnsNullAfterAllItemsAreIteratedThrough]
+[NEXT]: 0 >= 2
-[NEXT]: returning value
+[NEXT]: 1 >= 2
-[NEXT]: returning value
-[testNextReturnsNullAfterAllItemsAreIteratedThrough]
.... 60 / 88
..........................+[NEXT]: 0 >= 1
-[NEXT]: returning value
..
Time: 0 seconds, Memory: 4.25Mb
OK (88 tests, 326 assertions)
like I would expect to either get the same message in the output as I do in production or to at least a thrown exception or even a -[NEXT] return null but i get nothing but passing tests and I don't know why. Why???

Related

How to predefine variable in function by using different variable?

I dont know how to predefine one of the variables in function.
<?php
$number = 3;
function test($num, $num2 = $number) {
$result = "num = ".$num." and num2 = ".$num2;
return $result;
}
echo test(1);
?>
It always prints out:
Fatal error: Constant expression contains invalid operations in C:\xampp\htdocs\php\index.php on line 3
It's better to use it like this:
function test($num, $num2 = 3) {/**/}
But, if you really need it, you can define a constant:
const NUMBER = 3;
function test($num, $num2 = NUMBER)
{
$result = "num = $num and num2 = $num2";
return $result;
}
echo test(1); // returns "num = 1 and num2 = 3"
And the 3d option, if you want to use some dynamic variable:
$number = 3;
function test($num, $num2)
{
$result = "num = $num and num2 = $num2";
return $result;
}
echo test(1, $number); // returns "num = 1 and num2 = 3"
Or you can use a class:
class Test
{
protected $number;
public function __construct($number)
{
$this->number = $number;
}
public function test($num)
{
$result = "num = $num and num2 = $this->number";
return $result;
}
}
$test = new Test(3);
echo $test->test(1); // returns "num = 1 and num2 = 3"
If you want assingn a default value to a param you must use a value (a constant expression) not a var (variable expression)
function test($num, $num2 = 3 )

how to echo function in php like this print(h(pow)?

class fruits
{
function g($str = 'fruits'){
$i=0;
$new_str = "";
while ($i < strlen($str)-1){
$new_str = $new_str + $str[$i+1];
$i = $i + 1;
}
return $new_str;
}
function f($str = 'fruits') {
if (strlen($str)== 0) {
return "";
}
else if (strlen($str)== 1)
{
return $str;
}
else
{
return $this->f($this->g($str)) + $str[0]; }
}
function h($n=1, $str = 'fruits'){
while ($n != 1){
if ($n % 2 == 0){
$n = $n/2;
}
else
{
$n = 3*$n + 1;
}
$str = $this->f($str);
}
return $str;
}
function pow($x, $y){
if (y==0)
{
return 1;
}
else
{
return $x * $this->pow($x, $y-1);
}
}
}
$obj = new fruits;
print(h(pow());
I only want to ask how to echo a function like this print(h(pow);?
First turn on error reporting with:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
And you will see (Besides the typos):
Fatal error: Call to undefined function h() in ...
That is because you have a class with methods. So you have to take an instance of your class an call the method from it, e.g.
$obj = new fruits;
echo $obj->h($obj->pow(4, 5));
This is basic OOP PHP. Also I would highly recommed you to use more meaningful function and variable names!

php calculator class

I am stuck in resolving a PHP script.
I want to calculate a result from a set of instructions. Instructions comprise of a keyword and a number that are separated by a space per line. Instructions are loaded from file and results are output to the screen. Any number of Instructions can be specified. Instructions are operators (add, divide, subtract, multiply). The instructions will ignore mathematical precedence. The last instruction should be “apply” and a number (e.g., “apply 3”). The calculator is then initialised with that number and the previous instructions are applied to that number.
[Input]
add 2
multiply 3
apply 3
[Output]
15
this is what i have tried but i cant get the logic to complete the methods
class Calculator {
public $result = 0;
public $queue = Array();
public parseString($text) {
// parse input string
$cmds = explode(" ", $text);
foreach($cmds as $cmd) {
$cmd = trim($cmd);
if(!$cmd) continue; // blank or space, ignoring
$this->queue[] = $cmd;
}
// lets process commands
$command = false;
foreach($this->queue as $index => $cmd) {
if(is_number($cmd)) {
// if it's number fire previous command if exists
if(!$command || !method_exists($this, $command)) {
throw new Exception("Unknown command $command");
}
$this->$command($index, $cmd);
}else{
$command = $cmd;
}
}
}
public function apply($index, $number) {
// manipulate $result, $queue, $number
}
public function add($index, $number) {
// manipulate $result, $queue, $number
}
public function substract($index, $number) {
// manipulate $result, $queue, $number
}
}
$calculator = new Calculator();
$calculator->parseString('...');
how can i call or switch the add,divide,multiply,substract and how to distinguish and trigger apply word
any kind of help will be appreciated.
You should process the apply first and then cut it out of your queue array. Before you start looping through with the commands, simply test for the apply command and run it first. This simplifies the whole process.
After many minutes of experimentation and chatting, has been resolved.
<?php
error_reporting(E_ALL);
class Calculator {
public $result = 0;
public $queue = array();
public function parseString($text) {
// parse input string
$split = explode(" ", $text); //This gets your input into new lines
for ($i = 0; $i < count($split); $i += 2) $pairs[] = array($split[$i], $split[$i+1]);
foreach ($pairs as $bits) {
if ($bits[0] == "apply") {
$this->apply($bits[1]); //Set result equal to apply.
$this->queue[] = "output";
} else {
$this->queue[] = $bits; //Set the queue item as an array of (command, value).
}
}
//var_dump($this->queue);
//die;
// lets process commands
foreach ($this->queue as $index => $cmd) {
if ($cmd == "output") {
echo "Answer: " .$this->result;
return;
} else {
switch($cmd[0]) {
case "add":
$this->add($cmd[1]);
break;
case "subtract":
$this->subtract($cmd[1]);
break;
case "multiply":
$this->multiply($cmd[1]);
break;
case "divide":
$this->divide($cmd[1]);
break;
default:
echo "Unknown command!";
break;
}
}
}
}
public function apply($number) {
// manipulate $result, $queue, $number
$this->result = $number;
}
public function add($number) {
// manipulate $result, $queue, $number
$this->result += $number;
}
public function subtract($number) {
// manipulate $result, $queue, $number
$this->result -= $number;
}
public function multiply($number) {
// manipulate $result, $queue, $number
$this->result *= $number;
}
public function divide($number) {
// manipulate $result, $queue, $number
$this->result /= $number;
}
}
?>
Try using the array_shift and array_pop functions:
//pop the apply num off end off the queue
$result= array_pop($this->queue);
//now pop the apply operator off end of the queue
$operator = array_pop($this->queue);
//now get the first operator and numbers using array_shift
$operator = array_shift($this->queue); //get first operator
$num = array_shift($this->queue); //get first number
//loop perform operation on result using number till done.
while($num !== null)
{
$result = $operator($result, $num);
$operator = array_shift($this->queue);
$num = array_shift($this->queue);
}

Is it possible to reference an anonymous function from within itself in PHP?

I'm trying to do something like the following:
// assume $f is an arg to the wrapping function
$self = $this;
$func = function() use($f, $ctx, $self){
$self->remove($func, $ctx); // I want $func to be a reference to this anon function
$args = func_get_args();
call_user_func_array($f, $args);
};
Is it possible to reference the function assigned to $func from with the same function?
Try doing
$func = function() use (/*your variables,*/ &$func) {
var_dump($func);
return 1;
};
http://codepad.viper-7.com/cLd3Fu
Yes you can
See this example: http://php.net/manual/en/functions.anonymous.php#105564
Code from example:
<?php
$fib = function($n) use(&$fib) {
if($n == 0 || $n == 1) return 1;
return $fib($n - 1) + $fib($n - 2);
};
echo $fib(2) . "\n"; // 2
$lie = $fib;
$fib = function(){die('error');};//rewrite $fib variable
echo $lie(5); // error because $fib is referenced by closure
?>
Yes, it is possible if you use a variable by reference. For example:
$func = function($i) use(&$func) {
if ($i <= 0) {
return;
}
echo "Countdown: $i.\n";
$func($i - 1);
};
$func(3);

Calling a function within a class

I am having trouble calling a specific function within a class. The call is made:
case "Mod10":
if (!validateCreditCard($fields[$field_name]))
$errors[] = $error_message;
break;
and the class code is:
class CreditCardValidationSolution {
var $CCVSNumber = '';
var $CCVSNumberLeft = '';
var $CCVSNumberRight = '';
var $CCVSType = '';
var $CCVSError = '';
function validateCreditCard($Number) {
$this->CCVSNumber = '';
$this->CCVSNumberLeft = '';
$this->CCVSNumberRight = '';
$this->CCVSType = '';
$this->CCVSError = '';
// Catch malformed input.
if (empty($Number) || !is_string($Number)) {
$this->CCVSError = $CCVSErrNumberString;
return FALSE;
}
// Ensure number doesn't overrun.
$Number = substr($Number, 0, 20);
// Remove non-numeric characters.
$this->CCVSNumber = preg_replace('/[^0-9]/', '', $Number);
// Set up variables.
$this->CCVSNumberLeft = substr($this->CCVSNumber, 0, 4);
$this->CCVSNumberRight = substr($this->CCVSNumber, -4);
$NumberLength = strlen($this->CCVSNumber);
$DoChecksum = 'Y';
// Mod10 checksum process...
if ($DoChecksum == 'Y') {
$Checksum = 0;
// Add even digits in even length strings or odd digits in odd length strings.
for ($Location = 1 - ($NumberLength % 2); $Location < $NumberLength; $Location += 2) {
$Checksum += substr($this->CCVSNumber, $Location, 1);
}
// Analyze odd digits in even length strings or even digits in odd length strings.
for ($Location = ($NumberLength % 2); $Location < $NumberLength; $Location += 2) {
$Digit = substr($this->CCVSNumber, $Location, 1) * 2;
if ($Digit < 10) {
$Checksum += $Digit;
} else {
$Checksum += $Digit - 9;
}
}
// Checksums not divisible by 10 are bad.
if ($Checksum % 10 != 0) {
$this->CCVSError = $CCVSErrChecksum;
return FALSE;
}
}
return TRUE;
}
}
When I run the application - I get the following message:
Fatal error: Call to undefined
function validateCreditCard() in
C:\xampp\htdocs\validation.php
on line 339
any ideas?
class Foo {
// How may I be called?
function bar() {
}
function baz() {
// Use $this-> to call methods within the same instance
$this->bar();
}
function eek() {
// Use self:: to call a function within the same class statically
self::bar();
}
}
// Use [class]:: to call a class function statically
Foo::bar();
// Use [object]-> to call methods of objects
$fooInstance = new Foo();
$fooInstance->bar();
Calling methods statically or as an instance method is not necessarily interchangeable, beware. That's all pretty well covered in the basics of OOP by the way.
Does the class containing the function within which you use the Switch-Case inherit the CreditCardValidationSolution Class....??
My guess would be that you are trying to call the function outside the class without inheriting it.... Maybe you just missed it....
Edit After Reading Comment :
What you need here is "Inheritance"
Take a look at the following links.....
http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-4.php
http://marakana.com/blog/examples/php-inheritance.html
Hope this helps....

Categories