echo $user->data['user_id'];
// Outputs "2"
class AoW
{
public $user = new user();
public function __construct($phpBB_user);
{
$user = $phpBB3user;
}
}
$AoW = new AoW($user);
echo $AoW->user->data['user_id'];
// Would this output "2" as well?
Can an instance be a class attribute?
Page 1 of 19 Replies - 548 Views - Last Post: 18 April 2011 - 07:27 AM
#1
Can an instance be a class attribute?
Posted 12 April 2011 - 06:13 PM
My site runs on a phpBB3 backbone, which has various classes instantiated when a user logs on. One of these instances is $user, which contains a lot of handy properties and methods, but I want them to be accessible from within a "master" class for the site (which we'll call AoW(), instantiated as $AoW). Is it possible to pass $user as a variable when instantiating $AoW, and have the user() class methods available from the aow() class? I'll give an example below (of how I think it would be done)
Replies To: Can an instance be a class attribute?
#2
Re: Can an instance be a class attribute?
Posted 12 April 2011 - 06:28 PM
I think you're trying to do something like this:
<?php
class test {
public $x;
public function __construct($val) {
$this->x = $val;
}
function test($y) {
echo "This is a test: ".$this->x." | ".$y."<br>";
}
}
class myclass {
public $foo;
public function __construct($phpBB_user) {
$this->foo = $phpBB_user;
}
public function test_too() {
$this->foo->test("It works!");
}
}
$user = new test("some user");
$AoW = new myclass($user);
$AoW->test_too();
?>
#3
Re: Can an instance be a class attribute?
Posted 12 April 2011 - 06:32 PM
you could do
although that is not a sensible design (AoW->user could end up being anything).
to ensure that AoW has a user property, you need to pass it via setter/constructor:
by making it non-public, you ensure that it is not accidentally changed to an invalid value.
class AoW
{
public $user = null;
// …
}
$aow = new AoW();
$aow->user = new User();
although that is not a sensible design (AoW->user could end up being anything).
to ensure that AoW has a user property, you need to pass it via setter/constructor:
class AoW
{
protected $user = null;
public function __construct(User $user)
{
$this->user = $user;
}
public function getUser()
{
return $this->user;
}
}
$aow = new AoW(new User());
$uid = $aow->getUser()->getData('user_id');
by making it non-public, you ensure that it is not accidentally changed to an invalid value.
#4
Re: Can an instance be a class attribute?
Posted 12 April 2011 - 06:50 PM
Ah CT and Dorm, two of the reasons why I love DIC so much. Plusses all round!
#5
Re: Can an instance be a class attribute?
Posted 17 April 2011 - 03:56 PM
Okay, my first implementation of this isn't going so well, been fiddling with it for an hour or two, can't figure it out. I keep getting a fatal error "Call to a member function serveTemplate() on a non-object" on line 31 of this code:
PS This is stripped back code. In my original code title extends a different class, so can't extend template. Also, I'll need this sort of code structure later on when I start constructing game objects.
class template
{
public function serveTemplate(
$template,
$replaceArray
)
{
// Replace the {tags} from the $replaceArray
$template = preg_replace('/\{(\w+)\}/e', "\$replaceArray['\\1']", $template);
// Serve up replaced CSS
return $template;
}
}
class title
{
protected
$titleTemplate = null;
public function __construct()
{
$this->titleTemplate = new template();
}
public function displayTitles()
{
// Serve up the Titles
$template = file_get_contents(AOW_TEMPLATES . 'title' . FILEEXT_TEMPLATE);
$nullArray = array();
return $this->titleTemplate->serveTemplate($template, $nullArray);
}
}
$title = new title();
$title->displayTitles;
PS This is stripped back code. In my original code title extends a different class, so can't extend template. Also, I'll need this sort of code structure later on when I start constructing game objects.
This post has been edited by e_i_pi: 17 April 2011 - 03:58 PM
#6
Re: Can an instance be a class attribute?
Posted 17 April 2011 - 04:33 PM
From what I am seeing you should not be getting that error when running that code. If I were you my next step would be just adding print_r($this->titleTemplate) in the last line of the constructor and the first line of displayTitles() to ensure the object was instantiated correctly.
One strange thing I did notice is that you are not actually doing a method call: $title->displayTitles;, should be $title->displayTitles(); . I'm guessing that is something you wrote for demonstrative purposes and not the actual code you were running.
One strange thing I did notice is that you are not actually doing a method call: $title->displayTitles;, should be $title->displayTitles(); . I'm guessing that is something you wrote for demonstrative purposes and not the actual code you were running.
#7
Re: Can an instance be a class attribute?
Posted 17 April 2011 - 04:41 PM
When I change your last line to:
your code works for me. The only notices I get are about undefined constants and missing text file.
$title->displayTitles();
your code works for me. The only notices I get are about undefined constants and missing text file.
#8
Re: Can an instance be a class attribute?
Posted 17 April 2011 - 05:13 PM
Jstall, on 17 April 2011 - 05:33 PM, said:
From what I am seeing you should not be getting that error when running that code. If I were you my next step would be just adding print_r($this->titleTemplate) in the last line of the constructor and the first line of displayTitles() to ensure the object was instantiated correctly.
One strange thing I did notice is that you are not actually doing a method call: $title->displayTitles;, should be $title->displayTitles(); . I'm guessing that is something you wrote for demonstrative purposes and not the actual code you were running.
One strange thing I did notice is that you are not actually doing a method call: $title->displayTitles;, should be $title->displayTitles(); . I'm guessing that is something you wrote for demonstrative purposes and not the actual code you were running.
Yeah that was just a typo when I wrote up this code. The original code is across about 5 files.
I've checked print_r and it spits out gobbledegook. I think print_r is the father of regex:
template Object ( [_tpldata] => Array ( [.] => Array ( [0] => Array ( ) ) ) [_rootref] => [root] => [cachepath] => [files] => Array ( ) [filename] => Array ( ) [files_inherit] => Array ( ) [files_template] => Array ( ) [inherit_root] => [orig_tpl_storedb] => [orig_tpl_inherits_id] => [compiled_code] => Array ( ) )
Does that look right? I can't find any reference to the serveTemplate() method in there.
#9
Re: Can an instance be a class attribute?
Posted 17 April 2011 - 05:21 PM
I've found the problem. My site piggybacks off phpBB3, which already has the class template. I have to rename my class
#10
Re: Can an instance be a class attribute?
Posted 18 April 2011 - 07:27 AM
e_i_pi, on 17 April 2011 - 11:13 PM, said:
Yeah that was just a typo when I wrote up this code. The original code is across about 5 files.
I've checked print_r and it spits out gobbledegook. I think print_r is the father of regex:
I've checked print_r and it spits out gobbledegook. I think print_r is the father of regex:
print_r is one of the ways to get information about what a variable is referencing, var_dump is another option but I always found print_r more readable. It simply description of whatever it is called on along with any of it's data members. It gets more and more like gobbldy gook when are looking at an object utilizing composition/aggregation beceause then you see all their data members as well. You wouldn't see methods of the object.
I just recommended that to see why you were getting the call to non-object error. I wanted to see what datatype $title was
This post has been edited by Jstall: 18 April 2011 - 07:28 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|