Code: Select all
class someClass($val, $classInsance){
function someClass($val, $instance){
$this->val = $val;
$this->class = $insance;
$this->class->method('bla bla');
}
}Moderator: General Moderators
Code: Select all
class someClass($val, $classInsance){
function someClass($val, $instance){
$this->val = $val;
$this->class = $insance;
$this->class->method('bla bla');
}
}Code: Select all
<?php
class A {
function foo();
}
class B {
var $a;
function B($a) {
$this->a = $a;
}
function bar() {
$this->a->foo();
}
$a = new A;
$b = new B($a);
$b->bar();Code: Select all
<?php
class A {
function foo();
}
class B {
function bar() {
$a = new A;
$a->foo();
}
$b = new B;
$b->bar();Code: Select all
# Using extends :
class a {
Function foo() {
echo 'hello';
}
}
class b extends a {
function b() {
$this->foo();
}
}
# Using Isntantiation
class a {
Function foo() {
echo 'hello';
}
}
class b{
var $a;
function b() {
$this->a = new a;
$this->a->foo();
}
}
# Using non-instantiation:
class a {
Function foo() {
echo 'hello';
}
}
class b{
var $a;
function b() {
a::foo();
}
}I'd just like to point out the fact that you're attempting to pass arguments to the 'class' statement. The constructor someClass() can of course take an argument, whether it's an INT type, a string or another Object as you're indicating. More appropriately:The Ninja Space Goat wrote:Code: Select all
class someClass($val, $classInsance){ function someClass($val, $instance){ $this->val = $val; $this->class = $insance; $this->class->method('bla bla'); } }
Code: Select all
class someClass
{
// declare our object variables (PHP4 style)
var $val=null; // some value
var $class=null; // the object we're aggregating
/*
* constructor takes two arguments,
* a standard variable and another instantiated object
* Note passing the object by reference, as mentioned (PHP4)
*/
function someClass($val, &$instance){ // PHP4 style
$this->val = $val;
// assign the object variable
$this->class = $instance;
// utiilize one of the aggregated object's methods
$this->class->method('bla bla');
}
}Code: Select all
<?php
ob_start();
$id = $_COOKIE['rpg_id'];
$game = new Game($id);
$game->CheckRefresh();
$game->LoadPlayer();
$game->UpdatePlayer();
$game->CheckEvent();
$game->Render();
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
?>Not only is it correct, composition has become the prefered way for object to use other objects -- over inheritence. The both have there uses, but old-school OO used inheritance for everything.The Ninja Space Goat wrote:Can you do something like this? (I know this doesn't work because I tried it, but is it possible using correct syntax)?