| | |
How to reference one function from another from within a class in php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2009
Posts: 23
Reputation:
Solved Threads: 2
I am sorry to ask such a beginner question:
I am just learning to use classes and objects in php and would like to know one simple thing - how to you reference one function (or should I call them methods?) from another within the class. When I specify a return value, it gives me an error?
For example:
I know you probably wouldn't do it like this in this case, but if you were to have three functions like that, how to you set the one and call the other.
Where does the $this come in?
Most of the tutorials go to great length about explaining about how objects are created from classes and so on, but seem rather vague about the mechanics of the classes themselves. I also couldn't get a clear answer from looking through the classes/objects manual pages.
I am just learning to use classes and objects in php and would like to know one simple thing - how to you reference one function (or should I call them methods?) from another within the class. When I specify a return value, it gives me an error?
For example:
PHP Syntax (Toggle Plain Text)
class buttons{ function draw(){ echo "<input type="button" style='height:" . $size[height] . "; width:" . size[width] . ";' name='button' value='press' />" } function size(h, w){ $size[height] = h . 'px'; $size[width] = w . 'px'; } bringButton(h, w){ size(h, w); draw(); } } $LinkHome = new buttons; $LinkHome->bringButtons();
I know you probably wouldn't do it like this in this case, but if you were to have three functions like that, how to you set the one and call the other.
Where does the $this come in?
Most of the tutorials go to great length about explaining about how objects are created from classes and so on, but seem rather vague about the mechanics of the classes themselves. I also couldn't get a clear answer from looking through the classes/objects manual pages.
Last edited by peter_budo; Oct 23rd, 2009 at 2:30 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
•
•
Join Date: Apr 2009
Posts: 32
Reputation:
Solved Threads: 5
0
#3 Oct 23rd, 2009
PHP Syntax (Toggle Plain Text)
class buttons{ I've re-written your code and it should look like this hope it helps and if you get any bug let me know . function draw(){ echo "<input type='button' style='height:" . $size[height] . "; width:" . $size[width] . ";' name='button' value='press' />" } function size(h, w){ $size[height] = h . 'px'; $size[width] = w . 'px'; } bringButton(h, w){ $this->size(h, w); $this->draw(); } } $LinkHome = new buttons; $LinkHome->bringButton('12','14');
shalom shalom
•
•
Join Date: Apr 2009
Posts: 32
Reputation:
Solved Threads: 5
0
#4 Oct 23rd, 2009
PHP Syntax (Toggle Plain Text)
class buttons{ I've re-written your code and it should look like this hope it helps and if you get any bug let me know . function draw(){ echo "<input type='button' style='height:" . $size[height] . "; width:" . $size[width] . ";' name='button' value='press' />" } function size(h, w){ $size[height] = h . 'px'; $size[width] = w . 'px'; } bringButton(h, w){ $this->size(h, w); $this->draw(); } } $LinkHome = new buttons; $LinkHome->bringButton('12','14');
shalom shalom
•
•
Join Date: May 2009
Posts: 23
Reputation:
Solved Threads: 2
Thanks all for your input. I think I asked my question rather poorly. The example I gave I was just trying to cook up to try to understand something and I should have written it more carefully.
I don't want to post my real code simply because it's a lot more complex and there are other errors in there too that I need to work through.
Basically I build a blog engine a few months ago, after learning a good bit of basic PHP. But at the time I did it in order to learn some basics for a website I was building for a client. I left my blog working, but not great.
So now I have some time and I decided to get stuck into OOP with PHP.
I understand the basic concepts alright (I think.) The main problem that I am having is creating multiple references to functions. I'll try to give an example below:
User opens index page and the page loads until it gets to the blog post.
A object, $post, of postselector class gets created. Here there is a whole maze of things to negotiate. A counter sets it to the most recent post. I Use the counter to recognize the post by iterating through the database and finding it by the most recent time. So far all works fine.
Now I use that to call another function, choosing the post's title, time, and url (the actual post is in a .htm file). That part works (I can test it by calling it manually and commenting out the rest.
Then there are functions for recognizing the second post, the 10 most recent posts, posts by tags, and next and previous (blocking this one if counter is 1). There are also comments, which add a whole other dimension, because they work like mini blog posts on their own.
So it's complex. Now each peace works. I had it all working before, without using classes at all. Trying to find little problems in it made me understand the need for modularity, and it is obvious in this situation why it should work.
But what I seem to be doing now is trying to see weather I should set public variables or returns on the functions. I have tried (based on the advice in the tutorials I have worked through) to break every function that is doing to much into two, so that each is aiming to produce one thing. That's great, it makes it easy to follow my own code. But now I am really struggling to get it all together. As I try to fix, I am messing up my variables and database calls.
I am asking for advice more than anything. Should I be setting each one thing as a functions return value and referencing function after function? Can I stack it up? $postarray = $this->GetPosts($this->otherthing(1))
Should I try to create smaller classes all interacting.
I don't want to post my real code simply because it's a lot more complex and there are other errors in there too that I need to work through.
Basically I build a blog engine a few months ago, after learning a good bit of basic PHP. But at the time I did it in order to learn some basics for a website I was building for a client. I left my blog working, but not great.
So now I have some time and I decided to get stuck into OOP with PHP.
I understand the basic concepts alright (I think.) The main problem that I am having is creating multiple references to functions. I'll try to give an example below:
User opens index page and the page loads until it gets to the blog post.
A object, $post, of postselector class gets created. Here there is a whole maze of things to negotiate. A counter sets it to the most recent post. I Use the counter to recognize the post by iterating through the database and finding it by the most recent time. So far all works fine.
Now I use that to call another function, choosing the post's title, time, and url (the actual post is in a .htm file). That part works (I can test it by calling it manually and commenting out the rest.
Then there are functions for recognizing the second post, the 10 most recent posts, posts by tags, and next and previous (blocking this one if counter is 1). There are also comments, which add a whole other dimension, because they work like mini blog posts on their own.
So it's complex. Now each peace works. I had it all working before, without using classes at all. Trying to find little problems in it made me understand the need for modularity, and it is obvious in this situation why it should work.
But what I seem to be doing now is trying to see weather I should set public variables or returns on the functions. I have tried (based on the advice in the tutorials I have worked through) to break every function that is doing to much into two, so that each is aiming to produce one thing. That's great, it makes it easy to follow my own code. But now I am really struggling to get it all together. As I try to fix, I am messing up my variables and database calls.
I am asking for advice more than anything. Should I be setting each one thing as a functions return value and referencing function after function? Can I stack it up? $postarray = $this->GetPosts($this->otherthing(1))
Should I try to create smaller classes all interacting.
•
•
Join Date: Apr 2009
Posts: 32
Reputation:
Solved Threads: 5
0
#6 Oct 23rd, 2009
•
•
•
•
I am asking for advice more than anything. Should I be setting each one thing as a functions return value and referencing function after function?
![]() |
Similar Threads
- access JavaScript function return value from PHP (JavaScript / DHTML / AJAX)
- Pointer to function in Class (C++)
- about reference of a instance in class (C++)
- multi-file Class in PHP (PHP)
- stuck on a member function for a class (C++)
- How do I declare a class member function in another class? (Python)
- Javascript Function return value into PHP or not ?? Please help !! (JavaScript / DHTML / AJAX)
- How to code a friend template function? (C++)
Other Threads in the PHP Forum
- Previous Thread: hashing passwords - forgetful users?!
- Next Thread: Parse Error-- help!!!!
| Thread Tools | Search this Thread |
array bank c++ calling certifications class classes codeblocks compile convert courses file function functions if-else include java jobs linkednodes linuxfoundation memory news nodes parallel parameter passing php pong programming read reference return seminars snakes templates time training tutorial virtual void web wikipedia






