Skip to content
Advertisement

How to acces class attributes from another file in PHP

I want to access class attributes from another of my files but I don’t know how.

I am creating a class whose goal is to generate a CRUD table with the data passed in the parameters (table name, ID and table fields).

I have created the class in the file main.php, but I am also using an external file called res.php, this file will be in charge of making the calls to the database and building the table.

Once the class is finished and ready to be used, its function will be that in any file of all of my projects. If I instantiate an object of that class, a CRUD table will be created using the following:

$test = new GenerateCrud('users_test', 'id', ['usuario', 'apellido1', 'apellido2', 'email']);
$test->showTable();

My problem: To create the table, I have to access the attributes of the class, since I need to print the table name, fields, etc.

What happens to me? I don’t know how to do it.

My class file main.php:

<?php
        
    class GenerateCrud {
        
        // Properties.
        
            public $tableName;
            public $id;
            public $tableFields = array();
    
        // Constructor.
        
            function __construct($tableName, $id, $tableFields){
                $this->tableName = $tableName;
                $this->id = $id;
                $this->tableFields = $tableFields;
            }
    }
?>

My external file res.php:

<?php

    include_once('main.php');

?>  

        <div class="container caja">
            <div class="row">
                <div class="col-lg-12 col-sm-12">
                    <div>
                        <table id="tablaUsuarios" class="table table-striped table-bordered table-condensed" style="width:100%" >
                            <thead class="text-center">
                                <tr>
                                    <!-- CREATE FOR EACH TO SHOW THE TABLE FIELDS -->
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <script>
            $(document).ready(function() {
                var tableName = "<?php echo $this->tableName; ?>"; // HEREEEEEEEEEEEEEEEEEEE
                console.log(tableName);
            });
        </script>
<?php   
}

?>

I am aware that the variable $this does not point to anything, since I have not instantiated an object, but, then, is it as easy as instantiating an object and that’s it? I won’t get the information wrongly altered or anything like that? Won’t it cause me problems later when I instantiate the object again?

I know I’m not describing the problem very well and I’m aware that I lack knowledge, that’s obvious, that’s why I’m here asking to try to learn.

If someone can explain to me the reason for their explanation to get to understand this, I would appreciate it very much.

Thank you and have a nice day.

Advertisement

Answer

in order to access property tableName you have to initialize GenerateCrud object first. Nor in main.php nor in res.php I don’t see object initialization.

You said that to generate CRUD table you use

$test = new GenerateCrud('users_test', 'id', ['usuario', 'apellido1', 'apellido2', 'email']);
$test->showTable();

but where it is added in your code? is it in main.php or res.php I will suggest to you that you will add this peace of code to the top part of your main.php file and then you will be able to get tableName property

something like this

<?php

include_once('main.php');

$test = new GenerateCrud('users_test', 'id', ['usuario', 'apellido1', 'apellido2', 'email']);
?>

<div class="container caja">
    <div class="row">
        <div class="col-lg-12 col-sm-12">
            <div>
                <table id="tablaUsuarios" class="table table-striped table-bordered table-condensed" style="width:100%" >
                    <thead class="text-center">
                        <tr>
                            <!-- CREATE FOR EACH TO SHOW THE TABLE FIELDS -->
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function() {
        var tableName = "<?= $test->tableName; ?>"; // HEREEEEEEEEEEEEEEEEEEE
        console.log(tableName);
    });
</script>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement