0

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.

Edited by NardCake

6
Contributors
18
Replies
79
Views
4 Years
Discussion Span
Last Post by pritaeas
Featured Replies
  • 2

    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) … Read More

0

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.

Edited by LastMitch: grammer

0

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.

2

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');

Edited by pritaeas

0

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.

0

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.

0

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.

0

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?

0

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.

Edited by pritaeas

0

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?

Edited by diafol

0

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.

0

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.

0

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

Edited by NardCake

0

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.

0

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.

0

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!

0

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).

This question has already been answered. 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.