Object Oriented Predicament
Moderator: General Moderators
Object Oriented Predicament
So I'm trying to instantiate a custom class as a property of another class, yet I am getting an error stating that the 'new' keyword is out of place. Is it possible to have a handle of a custom class as a property of another? Any help is greatly appreciated.
-Your friendly stain
-Your friendly stain
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Object Oriented Predicament
Yes, but you can only assign constants, scalars and arrays to properties in their declarations. Assign the new instance in the __construct() method (or any other method).
(#10850)
Re: Object Oriented Predicament
Holey Toleto that was a colossal help! I would have never guessed such a thing haha.
Re: Object Oriented Predicament
Hey so I'm still having some trouble with my object oriented code. What I'm trying to do is make a tree of objects if that makes sense. Here is my code for my tree / node combination:
Every angle I look at it though, it looks fine. However, I keep getting an error with my code "$this->type->get_mult()", here is the error:
Fatal error: Call to a member function set_mult() on a non-object in /opt/lampp/htdocs/Errth/filter_lib/Tree.php on line 16
On my index file I have an autoloading function, so I know its not a lack of include files.
Any ideas? I could really use the help :S
Thanks!
-Your friendly stain
Code: Select all
<?php
class Tree
{
protected $type;
protected $children;
function __constructor()
{
$type = new NodeType();
}
//Assignment methods
function set_type_mult ($new_mult)
{
$this->type->set_mult($new_mult);
}
//Retrieval methods
function get_type_mult ()
{
return $this->type->get_mult();
}
}
?>Code: Select all
<?php
class NodeType()
{
protected $mult;
protected $rep;
function __constructor ()
{
$rep = new NodeRep();
}
//Assignment methods
function set_mult($new_mult)
{
$this->mult = $new_mult;
}
//Retrieval methods
function get_mult()
{
return $this->mult;
}
}
?>Fatal error: Call to a member function set_mult() on a non-object in /opt/lampp/htdocs/Errth/filter_lib/Tree.php on line 16
On my index file I have an autoloading function, so I know its not a lack of include files.
Any ideas? I could really use the help :S
Thanks!
-Your friendly stain
Re: Object Oriented Predicament
Whoops, sorry I forgot my other two object definitions.
This one really isn't important:
And on my index.php file I have these instantiations and method calls:
Code: Select all
<?php
class Node
{
protected $power, $active, $visible;
protected $tree;
function __constructor()
{
$tree = new Tree();
}
}
?>
Code: Select all
<?php
class NodeRep
{
protected $reps;
function __constructor()
{
$reps[0] = '';
$reps[1] = '';
$reps[2] = '';
$reps[3] = '';
}
}
?>
Code: Select all
$spew = new Tree();
$spew->set_type_mult(true);
echo $spew->get_type_mult();Re: Object Oriented Predicament
Code: Select all
function __construct() // __construct, not __constructor
{
$this->type = new NodeType(); // $this->type, not $type
}
Re: Object Oriented Predicament
Thanks Weirdan, but unfortunately it still gives the same error. It says that $this->type isn't an object that can use set_mult(), which is weird seeing now I have $this->type = new NodeType() in the constructor function :S
Would parenthesis help? Haha man I am so lost, this is quite different than C++. Thank you for your help though, it will probably solve a lot more issues in the future.
Basically what I am trying to do is make a tree of nodes that can be selected like a filter of some sorts. The mult variable is basically determining if the children are of a radio-button type or check-box type. This is all for a huge project that has some great goals:
-Eliminate the need to go to college to gain credit to your knowledge
-Replace the newspaper as a means of getting information that matters to you
-Meet intellectuals on the internet (like a facebook for smart folk)
-Become a replacement for both advertising agencies and hiring agencies
Hopefully I can get past this first hurdle so I can really get to coding ^_^
Thanks a bunch guys, all of you are awesome for holding this forum and being such avid users.
-Your friendly stain
Would parenthesis help? Haha man I am so lost, this is quite different than C++. Thank you for your help though, it will probably solve a lot more issues in the future.
Basically what I am trying to do is make a tree of nodes that can be selected like a filter of some sorts. The mult variable is basically determining if the children are of a radio-button type or check-box type. This is all for a huge project that has some great goals:
-Eliminate the need to go to college to gain credit to your knowledge
-Replace the newspaper as a means of getting information that matters to you
-Meet intellectuals on the internet (like a facebook for smart folk)
-Become a replacement for both advertising agencies and hiring agencies
Hopefully I can get past this first hurdle so I can really get to coding ^_^
Thanks a bunch guys, all of you are awesome for holding this forum and being such avid users.
-Your friendly stain
Re: Object Oriented Predicament
HOLY CRAP I just saw how it should be __construct. Man that fixed everything, thank you so much Weirdan. Hopefully I can land you the job of your dreams 
I love you all.
-Your friendly stain
I love you all.
-Your friendly stain
Re: Object Oriented Predicament
Is it possible to have a throw statement inside an object definition without it being surrounded by a try / catch combo? I was hoping you could so that way the only try / catch statements were in the main index file. Thank you all again, you guys are the <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>. Sorry if I seem out of place - I am a young hippie kid haha.
-Your friendly stain
-Your friendly stain
Re: Object Oriented Predicament
throw is allowed wherever statements are allowed. So, for example, you can have throw in a method body:Skidmark wrote:Is it possible to have a throw statement inside an object definition without it being surrounded by a try / catch combo?
Code: Select all
class A {
public function methodB($q = null) {
if (!is_null($q)) {
throw new InvalidArgumentException('$q is null and should stay that way');
}
}
}
Code: Select all
class A {
public function methodB() {}
throw new Exception; // <===== WRONG
public function methodA() { }
}
Re: Object Oriented Predicament
Great thanks again man that cleared up a lot of clutter for me.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Object Oriented Predicament
You might want to go to php.net and browse through the manual. It is excellent and the section on classes and objects will clarify many of your questions. It would be a half an hour well spent.
(#10850)
Re: Object Oriented Predicament
Christopher, yeah I did and you are right it did help out a lot, but it's more of a reference guide. It's difficult to look for information on an unknown concept haha.
But here is my next tackle; is it possible to define an expression with an expression without writing a new file?
But here is my next tackle; is it possible to define an expression with an expression without writing a new file?
Re: Object Oriented Predicament
Elaborate, please.Skidmark wrote:But here is my next tackle; is it possible to define an expression with an expression without writing a new file?
Re: Object Oriented Predicament
Alrighty, basically what I'm trying to do is take an XML file and translate it into a nested collection of objects. The way the PHP parser works, to my understanding, is event-based. What I have so far is with the set_element_handler function is when it sees the start of an element (in my XML file I only have two kinds of elements, one for check-box style children and one for radio-button style children), it creates a temporary object with all its properties assigned by the metadata of that element. Basically, what I need to do is place that temporary object inside the master object (provided in the parameters of the function I have defined that does the whole XML parsing operation), but with it's positioning exactly following that of the tree of XML elements. Here's how I was thinking of attacking it:
in the start element function, I would have it add another "->add_child()" / "->get_child()" combo, but I am just not sure yet, I feel like that would be a lot of wasted effort and time on both my part, and possibly the server's precious power. However, this is the only solution I have come up with so far. Idk man, my brain is fried hahaha.
in the start element function, I would have it add another "->add_child()" / "->get_child()" combo, but I am just not sure yet, I feel like that would be a lot of wasted effort and time on both my part, and possibly the server's precious power. However, this is the only solution I have come up with so far. Idk man, my brain is fried hahaha.