SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Make a procedural class?

    I would like to note, this is just to see if it can be done. I am not looking to do anything with it.
    Just as the title states, I would like to see if a PHP class can be made into a procedural function.

    Going by this link, going over the basics of OOP. I wanted to see if this could be done with regular functions:

    Here is the class from the site
    PHP Code:
    // class definition
    class Bear {
        
    // define properties
        
    public $name;
        public 
    $weight;
        public 
    $age;
        public 
    $sex;
        public 
    $colour;

        
    // constructor
        
    public function __construct() {
            
    $this->age 0;
            
    $this->weight 100;
            
    $this->colour "brown";
        } 

        
    // define methods
        
    public function eat() {
            echo 
    $this->name." is eating... ";
        }

        public function 
    run() {
            echo 
    $this->name." is running... ";
        }

        public function 
    kill() {
            echo 
    $this->name." is killing prey... ";
        }

        public function 
    sleep() {
            echo 
    $this->name." is sleeping... ";
        }


    // extended class definition
    class PolarBear extends Bear {

        
    // constructor
        
    public function __construct() {
            
    parent::__construct();
            
    $this->colour "white";
            
    $this->weight 600;
        }

        
    // define methods
        
    public function swim() {
            echo 
    $this->name." is swimming... ";
        }


    // my first bear
        
    $daddy = new Bear;
    // give him a name
        
    $daddy->name "Daddy Bear";
    // how old is he
        
    $daddy->age 8;
    // what sex is he
        
    $daddy->sex "male";
    // what colour is his coat
        
    $daddy->colour "black";
    // how much does he weigh
        
    $daddy->weight 300;

    // give daddy a wife
        
    $mommy = new Bear;
        
    $mommy->name "Mommy Bear";
        
    $mommy->age 7;
        
    $mommy->sex "female";
        
    $mommy->colour "black";
        
    $mommy->weight 310;

    // and a baby to complete the family
        
    $baby = new Bear;
        
    $baby->name "Baby Bear";

    // a nice evening in the Bear family
    // daddy kills prey and brings it home
        
    $daddy->kill();

    // mommy eats it
        
    $mommy->eat();
    // and so does baby
        
    $baby->eat();

    // mommy sleeps
        
    $mommy->sleep();
    // and so does daddy
        
    $daddy->sleep();

    // baby eats some more
        
    $baby->eat(); 

        echo 
    $baby->name." is ".$baby->colour." and weighs ".$baby->weight." units at birth"

    // create instance of Bear()
        
    $tom = new Bear;
        
    $tom->name "Tommy Bear";

    // create instance of PolarBear()
        
    $bob = new PolarBear;
        
    $bob->name "Bobby Bear";

    // $bob can use all the methods of Bear() and PolarBear()
        
    $bob->run();
        
    $bob->kill();
        
    $bob->swim();

    // $tom can use all the methods of Bear() but not PolarBear()
        
    $tom->run();
        
    $tom->kill();
        
    // $tom->swim(); Tom fails at swimming because it is a method of Polar Bear, not Bear 
    Instead of a bear, let's make a cat
    PHP Code:
    function construct$var$str ) {
        
    $var = !isset($var) ? $str $var;
        return 
    $var;
    }

    function 
    cat$array ) {
        
    // constructor
        
    $array['weight'] = construct$array['weight'], );
        
    $array['age']      = construct$array['age'], );
        
    $array['colour'] = construct$array['colour'], 'brown' );

        
    // properties
        
    return array(
            
    'name' => $array['name'],
            
    'weight' => $array['weight'],
            
    'age' => $array['age'],
            
    'sex' => $array['sex'],
            
    'colour' => $array['colour']
    //        ,'eats' => 'eating'
        
    );
    }

    // Extend Cat
    function tabbyCat$array ) {
        
    // constructor
        
    $array['weight'] = construct$array['weight'], 10 );
        
    $array['colour'] = construct$array['colour'], 'orange' );
        
        return 
    cat$array );
    }

    function 
    eating($name) {
        echo 
    $name." is eating... ";
    }

    function 
    runing($name) {
        echo 
    $name." is running... ";
    }

    function 
    killing($name) {
        echo 
    $name." is killing prey... ";
    }

    function 
    sleeping($name) {
        echo 
    $name." is sleeping... ";
    }

    function 
    pooping($name) {
        echo 
    $name." is pooping... ";
    }

    // 2 cats
        
    $morris cat(array('name' => 'Morris'));
        
    $heacliff cat(array('name' => 'Heacliff'));
    // and a kitten
        
    $kitten cat(array('name' => 'Baby Cat'));

    // a nice evening in the cat family
    // morris kills prey and brings it home
        
    killing$morris['name'] );

    // both eat the prey
        
    eating$morris['name'] );
        
    eating$heacliff['name'] );

    // both sleep
        
    sleeping$morris['name'] );
        
    sleeping$heacliff['name'] );

    // morris eats some more
        
    eating$morris['name'] ); 

    echo 
    $kitten['name']." is ".$kitten['colour']." and weighs ".$kitten['weight']." units at birth..."

    // ANOTHER CAT?!?!?!
        
    $hobbes tabbyCat(array('name' => 'Hobbes'));
            
    eating($hobbes['name']);
            
    pooping($hobbes['name']);
        
            
    // Morris gotta go too
            
    pooping($morris['name']);

    // Hobbes has a tag, let's get his infor
    // AGE is inherited from the parent 
        
    echo $hobbes['name'].', an '.$hobbes['colour'].' cat, weighed '.$hobbes['weight'].' units at birth at '.$hobbes['age'].' years old...'
    Obviously, I can't put the action functions in cat and then have it inherited by tabbyCat, I get a function already defined error. So i am trying to pass it via the array and call it as such - $morris['eats'](); This doesn't work as the parameter $name is not called.

    So my question is, short of me placing the functions in the function, how can I call up the method functions from the parent function? I was thinking of adding another array with the function name and parameter and doing an in_array with call_user_func, but I tried something similar and it caused the code to be run when cat() was called. That is the other issue, allowing the code to run when it is called, not when the parent function is called.

    Either way, it is just to see if it can be done.

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would you even WANT to do this?

  3. #3
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Again, just to see if can be done. I don't think it can be done without setting some rules and having extra functions to allow it to happen. The part that gets me is having the parent function note what "methods" to be called, and run then when they are called like a class.

    Like I said it just to see if it can be done. I will not be using it in any project or anything. If I need a class, I can always use one.

  4. #4
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The short answer: No.

    You cannot use all OOP functionality without using classes, no matter how hard you try.

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rather one can use prefix in function name in order to avoid collisions
    Like
    Bear_eat()
    Bear_run()
    ...


Bookmarks

Posting Permissions

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