...

View Full Version : classes



Phil Jackson
09-01-2009, 12:21 PM
Morning all.
Right, iv'e never ever had to write a class so i need some help (even though iv'e used em a million times).

I am writing my own form of encryption and would like to use this to increase my knowledge of classes.



<?php

class encryption
{
function checkKEY($data){
if(strlen($data)>=16)
$result = 0;
else
$result = 1;
return $result;
}
function encrypt()
{
if($this===0)
{
}
else
{
return "error: key must be 16 bit";
}
}
}

$myclass = new encryption;
echo $myclass->checkKEY("%$&$*&^%")->encrypt;

?>


current error is :Notice: Trying to get property of non-object in C:\wamp\www\help\2.php on line 25 (the last line of what i wrote)

am i right in assuming the result of checkKEY("%$&$*&^%") gets passed onto the encrypt function?

funnymoney
09-01-2009, 12:48 PM
<?php

class encryption
{
function checkKEY($data){
if(strlen($data)>=16)
$result = 0;
else
$result = 1;
return $result;
}
function encrypt()
{
if($this===0)
{
}
else
{
return "error: key must be 16 bit";
}
}
}

$myclass = new encryption;
echo $myclass->checkKEY("%$&$*&^%")->encrypt;

?>


if i understand you right, you need something like this


<?php

class Encryption
{
function checkKEY($data){
if(strlen($data)>=16)
$result = 0;
else
$result = 1;
return $result;
}
function encrypt($data)
{

if(!$this->checkKEY($data))
{
$enctype = "error: key must be 16 bit";
}
else {
$enctype = "OK";
}
return $enctype;
}
}

$myclass = new Encryption();

echo $myclass->encrypt("%$&L$*&^%");
?>

Phil Jackson
09-01-2009, 01:33 PM
if(!$this->checkKEY($data)) to if($this->checkKEY($data))
but yup, your right!

What is meant when i see classes used such as


$foo->$bar->$foobar->$barfoo();??

funnymoney
09-01-2009, 01:46 PM
$foo->$bar->$foobar->$barfoo();??

if you are calling $foo->bar you are calling bar construct of $foo class.



class Foo {
public function __construct() {
$this->bar = "Class foo construct";
$this->foobar = $this->foomethod();
}
public function foomethod() {
return "From foomethod()";
}
}

$foo = new Foo();

print $foo->bar;
print $foo->foobar;


I edited function name from foo() to foomethod() cause it might give you some problems because before in PHP4 when you wanted to create construct you would just type a function with same name as a class and then it would behave like construct. but, let's just avoid using same function and classes names. inside the same class of course.

bacterozoid
09-01-2009, 02:17 PM
That example is a little confusing, I think. If you had


$foo->bar->foobar->barfoo();

This is what you would expect to see.


<?php

// Foo class, has variable $bar
class Foo {
var $bar;
}

// Bar class, has variable $foobar
class Bar {
var $foobar;

}

// FooBar class, has function barfoo();
class FooBar {
function barfoo() {
echo 'barfoo';
}
}

$foo = new Foo(); // Create a new Foo()
$foo->bar = new Bar(); // Set var $bar in Foo() to a new Bar() object
$foo->bar->foobar = new Foobar(); // Set var $foobar in Bar to a new FooBar() object

$foo->bar->foobar->barfoo(); // Starting at Foo, traverse down until you can finally call the barfoo function.

?>

Phil Jackson
09-01-2009, 03:01 PM
<?php

class Encryption
{
function checkKEY($data){
if(strlen($data)>=16)
$result = 0;
else
$result = 1;
return $result;
}

function encryptKey($data)
{

if($this->checkKEY($data))
{
die ("error: key must be 16 bit");
}
else
{
return TRUE;
}
}
}
class Encryption2
{
var $encrypt;
function dataToEncrypt($data)
{
return $data." - ".$encrypt; //just to test
}
}

$data = "I would love to go for a walk in the park";
$encrypt = new Encryption();
$next = $encrypt->encryptKey("password1234567890");
$encrypt->dataToEncrypt = new Encryption2();
echo dataToEncrypt($data);

?>


Am i on the right lines here?
getting error: Call to undefined function dataToEncrypt()

Phil Jackson
09-01-2009, 03:03 PM
<?php

class Encryption
{
function checkKEY($data){
if(strlen($data)>=16)
$result = 0;
else
$result = 1;
return $result;
}

function encryptKey($data)
{

if($this->checkKEY($data))
{
die ("error: key must be 16 bit");
}
else
{
return TRUE;
}
}
}
class Encryption2
{
function dataToEncrypt($data, $next)
{
return $data." - ".$next; //just to test
}
}

$data = "I would love to go for a walk in the park";
$encrypt = new Encryption();
$next = $encrypt->encryptKey("password1234567890");
$encrypt2 = new Encryption2();
echo $encrypt2->dataToEncrypt($data, $next);

?>

this worked tho

bacterozoid
09-01-2009, 03:11 PM
FYI:


die ("error: key must be 16 bit");

"password1234567890" or any other alphanumeric string of that length is actually 128 bits. Each character is 8 bits.

In binary


"password1234567890" = 0111000001100001011100110111001101110111011011110111001001100100001100010011001000110011001101000011 01010011011000110111001110000011100100110000

Each 1 or 0 is a bit (when encoded in binary).

Therefore, a 16 bit encryption key would be like "pa".

Phil Jackson
09-01-2009, 05:52 PM
anyone know a function to convert string to binary?

Fou-Lu
09-01-2009, 06:57 PM
anyone know a function to convert string to binary?

I don't think php has a built in command for this, though I know they will (or perhaps already have) implemented a binary cast.
What you can do is take an array of chars, convert them to ascii, and than convert each char to binary.
Something like this:


function strbin($s)
{
if (!is_string($s))
{
throw new InvalidArgumentException('Argument must be a string value!');
}
$aChars = array();
$iLength = strlen($s);
for ($i = 0; $i < $iLength; ++$i)
{
$aChars[] = str_pad(decbin(ord($s[$i])), 8, '0', STR_PAD_LEFT);
}
return implode(' ', $aChars);
}

Phil Jackson
09-01-2009, 11:51 PM
not looked at yet but thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum