Hello! I'm working on a mini framework and everything is going great, exvept this one part that I have gotten working but it brings more complications and it's extremely confusing to me. So I will summarize what is going on. Essentially I have a class (a controller, we'll call this class A) that is extending another class (Class B), in class A I want the ability to say $this->fetch->view('main'); which I have gotten working, but it was of course taking place outside of class B and class A. I would like this to take place in class B somehow so these methods can change properties and such. I had an idea to somehow have it when the fetch class is referred to point back to class B so the methods could be contained in class B? I don't know if that makes any sense. I will try to illustrate it in code.

// So basically I would like to stylize this
$this->view('main');
//Into this
$this->fetch->view('main');
And maybe this is what whould happen by the scenes? (pseudo code)
class fetch {
    WHEN I'M CALLED I'M GOING TO PASS IT ON TO YOU CLASS B
}
class B {
    OH THANKS FOR THE PASS I WILL CALL THESE METHODS NOW, BUDDY

    public function view(){
        //stuff
    }
}

Hopefully that makes some sense...
Thanks for any answers or suggestions.

Recommended Answers

All 18 Replies

Member Avatar for LastMitch

I had an idea to somehow have it when the fetch class is referred to point back to class B so the methods could be contained in class B? I don't know if that makes any sense. I will try to illustrate it in code.

I don't want to be rude. Not really. Is this a school project? I don't quite understand what you are trying to do.

What does class fetch mean?

Are you creating a View modal or fetching data from the database?

Maybe Switch statement something like this (I just wrote some comments with the switch):

$this->fetch->view('main');

$type = 'main';
$type1= 'main1';

class fetch{

var $type;
var $type1;

public function fetch($type = 'main')
    {
        if (isset($this->result))
        {   //switch
            switch ($type)
            {   //if this is the case is you want select
                case 'main':

                //This will fetch a row as array
                $row = $this->result->fetch_array();

                break;
                //if the case above not select then this case you want to select 
                case 'main1':

                //This will fetch a row as array
                $row = $this->result->fetch_array();

                default:

                //This will fetch a row as object
                $row = $this->result->fetch_object();    

                break;
            }

            return $row;
        }

        return FALSE;
    }
  }
} 

If that's not close what you are talking about then you have to explain it more detail and if you have a little more code it would be helpful.

It's not a school project and it is a view model. I will try to explain it in more detail. I have the class that controllers will extend, class A. I also have the controller class itself that will extend class A, we will call this class B. In class B I would like to be able to do $this->fetch->view('main'); (other methods than view eventually) The thing is I can't simply create a class outside of Class A and instantiate it since the methods that would be inside that class require access to class A which will change things in Class B since it's extending it. So I had an idea so when a method is called inside the fetch class call the same method but back in class A, would that work?

//I would like this in class B for example
$this->fetch->view('main');
//but it's really the equivalent of doing this.
$this->view('main');
//Mainly for organization,stylization, and less collisions.

Thanks for any suggestions, and please ask if more clarification and ore examples are needed.

One way is to include a reference to the other class in your call (a callback), but perhaps you're looking for method overriding. Without your actual class definitions it's hard to guess which direction would be the right one.

Is this what you have/want?

class Controller {
    public function fetch($arg) {
        $this->view($arg);
    }
}

class B extends Controller {
    public function view($arg) {
        echo $arg;
    }
}

$b = new B();
$b->fetch('main');
Member Avatar for diafol

I'm confused with your use of $this->... as you say this occurs outside your classes. How so? $this-> AFAIK is for methods and props from within classes.

Is this using ZF?

This is waay harder to explain than expected lol. @Squidge assuming ZF means ZendFramework, no I am not using it, I'm noy using any framework. I'm actually working on my own for me and my friends projects it's working extremely well I'm just trying to get it a bit more organized and semantic.
This time I'm to ask an extremely simple question that will need to be solved at the base of the problem. Everything is working the way I currently have it, it's just a bit less organized. Is there someway in a class to forward everything to another class? That's the simplest I can get.

That is exactly what extends does in the class B for the example pritaeas had given. It is able to use everything that is available in the Controller class and manipulate the output in class B without affecting the original Controller class.

Now I'm starting to question my explanation because that is not at all what I mean.

<?php
/* Example controller. */
class example extends controller {
    public function __construct(){
        /* How I would like to fetch views */
        $this->fetch->view('example');
    }
}
/* The controller class itself */
class controller {
    public $fetch;
    public function __construct(){
        $this->fetch = new fetch();
    }
}
class fetch {
    //somehow forward to the controller class so changes can be made. (Not specifically
    for the view method though)
    public function view(){

    }
}

Does that clear anything up?

The code above works. Anything that is publicly available in fetch, you can use like that in example. Still unsure what you are looking for. Perhaps you need to look at interfaces. An interface basically defines a common method for objects to interact.

Member Avatar for diafol

SO the controller class is abstract?
Do the publics in controller need to be protected? Maybe overkill?
Also, I think you need to add the parent constructor as you may be overriding the method otherwise and not get the $fetch obj.

<?php
/* Example controller. */
class example extends controller {
    public function __construct(){
        parent::__construct();
        $this->fetch->view('example');
    }
}
/* The controller class itself */
abstract class controller {
    protected $fetch;
    protected function __construct(){
        $this->fetch = new fetch();
    }
}
class fetch {
    public function view(){
        echo "yey";
    }
}

$ex = new example;
 ?>

Also a bit confused - will you be creating a custom extended controller for each view?

That's not the problem though, I want methods in the view class to have the ability to have access to properties and such in the controller class whichj in turn will be accessible by the example class. That's why I wanted the fetch class to somehow redirect back to the controller class, if that makes sense.

Member Avatar for diafol

if that makes sense.

Not really. If you show a bit of code (methods or properties) from your controller class that you want to be accessible in your view class (whatever that is), and how the view class would use it, it may help.

For example in my example class

<?php
class example extends controller {
    public function __construct(){
        //fetch a library
        $this->fetch->lib('example');
    }
}

That method would include a class, instantiate it and set it equal to a property so that class could later be accessed as such

$this->example->method('this is an example');

Does that make a little more sense?
Thanks for your effort to understand this guys, and your quick responses :P

Yes I have and can do that, it's just that the view class needs to have access to a property in the controller class so I can set it equal to the object.

You can always pass the controller object as a parameter to the new class' constructor. That way you can always access it.

If that's still not it, perhaps, instead of trying to explain the problem in your solution, trying to explain the original problem you are trying to solve may help. Step back, and try to explain the bigger picture.

Again I'm so sorry that this is confusing to you guys.
Thank you pritaeas for some reason I never thought of that, that's just what I needed and it's quite simple. After maybe a little to much meaningless struggle with explanation I have got it. Thanks everyone!

Am glad that solved it, but be very careful with that. You can (ab)use this to create OO spaghetti, creating interweaving classes. I still think it's a design problem, this shouldn't be your solution (unlikely).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.