Welcome Guest, Not a member yet? Register   Sign In
I couldn't create my own class inside a controller
#1

[eluser]Jose Dueñas[/eluser]
Hi,
I would like to create a class called "Article" inside a cotroller called "Welcome". I get a PHP error: "PHP Parse error: syntax error, unexpected T_CLASS, expecting T_FUNCTION".
This is my code:

Code:
<?php

class Welcome extends Controller {
    var $css;
    
    class Article{
        var $titulo = array();
        var $categoria = array();
        var $contenido = array();
    }


    function Welcome()
    {
        parent::Controller();    
    }

    function index()
    {
        $this->load->view('welcome',$data);
    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

I will use the Article class in some functions of this controller.

Thanks,
Jose
#2

[eluser]Philipp Datz[/eluser]
the controller itself is already a class
#3

[eluser]Jose Dueñas[/eluser]
Thanks Philipp,
so there is no way to crete my own classes inside the controllers?

Regards,
Jose
#4

[eluser]Philipp Datz[/eluser]
no, if you want to create your own class have a look at libtraries/helpers. but first you have to think about whether you really need a class besides the mvc-pattern.

philipp
#5

[eluser]Elliot Haughin[/eluser]
If it was essential that you had an extra class, this might work (untested)
Code:
<?php

    class Article {
        var $titulo = array();
        var $categoria = array();
        var $contenido = array();

        function Article()
        {
           // Word! this is the constructor!
        }
    }

class Welcome extends Controller {
    var $css;
  
    function Welcome()
    {
        parent::Controller();

        $this->article = new Article();
    }

    function index()
    {
        $something = $this->article->contenido;
        $this->load->view('welcome',$data);
    }

}

Again, not recommended, but it might work.
#6

[eluser]Developer13[/eluser]
Just create your class as a library.
#7

[eluser]marcoss[/eluser]
[quote author="Jose Dueñas" date="1225666257"]Hi,
I would like to create a class called "Article" inside a cotroller called "Welcome". I get a PHP error: "PHP Parse error: syntax error, unexpected T_CLASS, expecting T_FUNCTION".
This is my code:

Code:
<?php

class Welcome extends Controller {
    var $css;
    
    class Article{
        var $titulo = array();
        var $categoria = array();
        var $contenido = array();
    }


    function Welcome()
    {
        parent::Controller();    
    }

    function index()
    {
        $this->load->view('welcome',$data);
    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

I will use the Article class in some functions of this controller.

Thanks,
Jose[/quote]

Not a CI issue, PHP doesn't support nested classes.

Create a file MY_Controller.php on your app/libraries folder and put your extra clases there, you can even extend the controller class and use the custom controller directly.

Code:
class ArticleController extends Controller
{
    public $titulo = array();
    public $categoria = array();
    public $contenido = array();
    
    public function __construct()
    {
        parent::__construct();
    }
}
#8

[eluser]Jose Dueñas[/eluser]
Thanks to everyone for your help.
I've realized in PHP you cannot use nested classes as marcoss said (this has been an epic FAIL for me, hehe).
Elliot's answer could be a solution, I like it, I thought about create a library as D13 said, but I'd prefer something more simple.

My problem mainly is: My app has a section listing all the articles in te database, but i want to show in the frontpage, last 10 articles. If I have defined a "Article controller" in index.php/article/show, how could I use that controller to show last 10 articles in the frontpage (index.php)?
So I cannot use the article controller inside the frontpage controller, so I decided to create another article class inside frontpage controller...
Well, I think I'm messing a bit.. hehe

Elliot, I'm one of your followers (Twitter), thanks also for your CodeIgniter libraries, i'm using them in some of my projects. You're great man!
#9

[eluser]Colin Williams[/eluser]
A class that interacts with the database... that's what a Model is.
#10

[eluser]drewbee[/eluser]
Quote:Not a CI issue, PHP doesn’t support nested classes.

Wow... Packages ftw? Wonder if that is the next big thing for PHP.... Smile


Jose...

Your model is the file that has all of the data retrieving abilities from the database.

Keep in mind, for every form (that has data interaction), you will have three files.

The Controller, the model, and the view.

The view is your presentation (HTML), the model is your data abstraction layer (databases, file IO, and general data manipulation), and then think of the controller that acts as the glue between the two (as well as providing data validation).

What you want to do is initiate your model in your controller Smile




Theme © iAndrew 2016 - Forum software by © MyBB