SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: Make a procedural class?
Hybrid View
-
Nov 3, 2009, 18:03 #1
- 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.
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
PHP Code:function construct( $var, $str ) {
$var = !isset($var) ? $str : $var;
return $var;
}
function cat( $array ) {
// constructor
$array['weight'] = construct( $array['weight'], 5 );
$array['age'] = construct( $array['age'], 1 );
$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...';
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.
-
Nov 5, 2009, 02:22 #2
- 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?
Turnware MVC - A new barebone, fast and flexible PHP5 framework!
Ruben Knol
Turnware MVC Lead Developer
-
Nov 5, 2009, 07:08 #3
- 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.
-
Nov 5, 2009, 08:01 #4
- 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.Turnware MVC - A new barebone, fast and flexible PHP5 framework!
Ruben Knol
Turnware MVC Lead Developer
-
Nov 6, 2009, 02:40 #5
Rather one can use prefix in function name in order to avoid collisions
Like
Bear_eat()
Bear_run()
...
Bookmarks