Search
 
SCRIPT & CODE EXAMPLE
 

PHP

class constants in php

<?php
class MyClass
{
    const CONSTANT = 'constant value';

    function showConstant() {
        echo  self::CONSTANT . "
";
    }
}

echo MyClass::CONSTANT . "
";

$classname = "MyClass";
echo $classname::CONSTANT . "
";

$class = new MyClass();
$class->showConstant();

echo $class::CONSTANT."
";
?>
Comment

php call constant in class

//it's possible to declare constant in base class, and override it in child, and access to correct value of the const from the static method is possible by 'get_called_class' method:
<?php
abstract class dbObject
{   
    const TABLE_NAME='undefined';
   
    public static function GetAll()
    {
        $c = get_called_class();
        return "SELECT * FROM `".$c::TABLE_NAME."`";
    }   
}

class dbPerson extends dbObject
{
    const TABLE_NAME='persons';
}

class dbAdmin extends dbPerson
{
    const TABLE_NAME='admins';
}

echo dbPerson::GetAll()."<br>";//output: "SELECT * FROM `persons`"
echo dbAdmin::GetAll()."<br>";//output: "SELECT * FROM `admins`"

?>
Comment

PREVIOUS NEXT
Code Example
Php :: php foreach loop first element 
Php :: login form using php pdo 
Php :: how to upgrade php in mac mojave 
Php :: Add Text Before the Product Title 
Php :: minishlink/web-push v5.2.5 requires ext-gmp * 
Php :: Delete a single record in laravel 5 
Php :: seprate day and year from laravel to timestamp 
Php :: session() in lumen 
Php :: PHP str_word_count — Return information about words used in a string 
Php :: create a laravel project 
Php :: laravel observer get old value 
Php :: Difference in seconds between datetime 
Php :: wordpress plugin functions exist 
Php :: laravel with and where 
Php :: wp php get rows number from mysql 
Php :: laravel hash namespace 
Php :: php mail if successful 
Php :: dependable validation in laravel 
Php :: how to save data from api to laravel 
Php :: html in php function 
Php :: laravel documentation updateOrCreate 
Php :: php mysql prepared statements 
Php :: merge array in php 
Php :: how to use attempt in laravel 
Php :: download xampp php 7.3 
Php :: php preg_quote 
Php :: laravel project make 
Php :: php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider 
Php :: laravel filter get pagiination does not flter Appending To Pagination Links 
Php :: laravel pagination with search filter 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =