Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: classes

  1. #1
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts

    classes

    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 Code:
    <?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?

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by Phil Jackson View Post
    PHP Code:
    <?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 Code:
    <?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$*&^%");
    ?>

  • Users who have thanked funnymoney for this post:

    Phil Jackson (09-01-2009)

  • #3
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    if(!$this->checkKEY($data)) to if($this->checkKEY($data)) 
    but yup, your right!

    What is meant when i see classes used such as

    PHP Code:
    $foo->$bar->$foobar->$barfoo(); 
    ??

  • #4
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by Phil Jackson View Post
    PHP Code:
    $foo->$bar->$foobar->$barfoo(); 
    ??
    if you are calling $foo->bar you are calling bar construct of $foo class.

    PHP Code:
    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
    Edit: 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.
    Last edited by funnymoney; 09-01-2009 at 02:13 PM.

  • Users who have thanked funnymoney for this post:

    Phil Jackson (09-01-2009)

  • #5
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    That example is a little confusing, I think. If you had

    PHP Code:
    $foo->bar->foobar->barfoo(); 
    This is what you would expect to see.

    PHP Code:
    <?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.

    ?>

  • Users who have thanked bacterozoid for this post:

    Phil Jackson (09-01-2009)

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?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()

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?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

  • #8
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    FYI:

    Code:
    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

    Code:
    "password1234567890" = 011100000110000101110011011100110111011101101111011100100110010000110001001100100011001100110100001101010011011000110111001110000011100100110000
    Each 1 or 0 is a bit (when encoded in binary).

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

  • Users who have thanked bacterozoid for this post:

    Phil Jackson (09-01-2009)

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    anyone know a function to convert string to binary?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Phil Jackson View Post
    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:
    PHP Code:
    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);

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Phil Jackson (09-01-2009)

  • #11
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    not looked at yet but thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •