0

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:

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.

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by omoabobade
0

Make this modifications and try,

$LinkHome = new buttons();


and the function should be called using $this->draw(); and $this->size();

0
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

0
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

0

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.

0

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?

if you want easy readability of your code i'll suggest you do that though my practice might not be the best but it works fine and makes my code easy to read (open to criticism).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.