tags:

views:

160

answers:

5

If a method creates an object and I call the method from an other object, will the last object have access to the first object's properties and methods?

A: 

If it is returned/stored somewhere, public fields and methods will be accessible.

Nathaniel Flath
+1  A: 

There's some extraneous information there that may be confusing you.

The method and the object (in this case) are disconnected from one another. So the question becomes, are you storing the created object in a scope that the second object has access to?

Boo
A: 

Edit: In light of the change of tags, this answer is no longer relevant. I've left it to preserve the comments...

Original Answer:

Like this?:

public MyObject CreateObject()
{
   return new MyObject() { FirstProperty = "Hello World" };
}

public Main()
{
    MyObject n = CreateObject();
    Console.WriteLine(n.FirstProperty);
}

Or this?:

class Program
{
    MyObject _myObject;

    public void CreateObject()
    {
        _myObject = new MyObject() { FirstProperty = "Hello World" };
    }

    public Main()
    {
        Console.WriteLine(_myObject.FirstProperty);
    }
}

In either of these two cases, sure you can access properties of your object. If this is not what you meant, I'm not sure exactly how to answer your question and you'll need to clarify.

BenAlabaster
In Router, in a method, in an included file is the controller code. In the model and in the view, I need access to the Controller's properties and methods. In Router, in an other method, I want to return a controller object to the model and the view. What now???
So you're using ASP.NET MVC?
BenAlabaster
I don't understand the CakePHP source code, so I was having to ask help to configure it all the time. Plus really lacking social skills, the CakePHP community stopped helping me. Now, I need to work with pure PHP.
Okay, I retagged your question - my response isn't really valid for PHP/CakePHP hopefully this will draw more relevant responses for you.
BenAlabaster
A: 

Only if the method keeps a reference to the object that it creates.

McWafflestix
+1  A: 

In Router, in a method, in an included file is the controller code. In the model and in the view, I need access to the Controller's properties and methods. In Router, in an other method, I want to return a controller object to the model and the view. What now??

If I understand the question properly, you're a bit confused about MVC. Router class is a cake internal class and should never never ever never never absolutely never ever be changed. And those "never ever" are not even copy-pasted, they are really typed.

Second, model classes don't even know anything called controller. Controller uses models, not the other way around. If your model needs something from a controller, pass it on as a parameter. Anything beyond that is just bad design.

Also, calling controller actions from a view is possible, but strongly discouraged. Controller is the one to prepare all the data for a view, therefore view has no need to access the controller (there are exceptions to this, out of scope of this question).

I recommend you read a bit about MVC, cake's typical request, and at least go through the basic blog tutorial.

dr Hannibal Lecter