Constructor Archives | phpGrid - PHP Datagrid Create PHP grids in minutes, not hours. Fri, 06 Dec 2024 02:39:49 +0000 en-US hourly 1 CodeProjecthttps://wordpress.org/?v=7.0.3 129966043 PHP 8: Constructor property promotion https://phpgrid.com/blog/php-8-constructor-property-promotion/ Mon, 28 Feb 2022 18:41:51 +0000 https://phpgrid.com/?p=9895

It’s typical for constructor arguments to be allocated to a property in the constructor but never used. PHP 8 added constructor promotion as a convenient shorthand for such scenario. Below is a typical constructor pre PHP 8: 1234567891011class Order {     public int $id;     public string $description;     public function __construct(string […]

The post PHP 8: Constructor property promotion appeared first on phpGrid - PHP Datagrid.

]]>

It’s typical for constructor arguments to be allocated to a property in the constructor but never used. PHP 8 added constructor promotion as a convenient shorthand for such scenario.

Below is a typical constructor pre PHP 8:

1
2
3
4
5
6
7
8
9
10
11
class Order
{
    public int $id;

    public string $description;

    public function __construct(string $id, string $description) {
        $this->id = $id;
        $this->description = $description;
    }
}

It seems just repetitive work with no real benefits. Thankfully, starting PHP 8.0, when a visibility modifier is included in a constructor argument, PHP treats it as both an object property and a constructor argument, and assigns the argument value to the property.

So you can now write this in PHP 8:

1
2
3
4
5
6
class Order
{
    public function __construct(public int $id, public string $description)
    {
    }
}

With much cleaner and shorter syntax, property promotion allows you to integrate class fields, constructor definitions, and variable assignments – all into a single syntax.

The Promoted properties can have default value

1
2
3
4
public function __construct(
    public int $id = 1,
    public string $description = 'foo'
) {}

It’s OK to combine promoted and normal properties

1
2
3
4
5
6
7
8
9
10
11
class Order
{
    public  $description;

    public function __construct(
        public int $id,
        string $description
    ) {
        $this->description = $description;
    }
}

It’s important to note that you can’t have the same name for a class property and a promoted property.

Do not use ‘var’

Finally, when declaring class variables for constructor property promotion, do not use ‘var’. Only the terms “public,” “protected,” and “private” are permitted.

So the following is not a valid constructor property promotion:

1
public function __construct(var string $foo) {}

That’s all there is to it. The constructor property promotion makes your code cleaner, shorter, and more awesome! Learn to start using it today.

Recommended read:
https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion

The post PHP 8: Constructor property promotion appeared first on phpGrid - PHP Datagrid.

]]>
9895
phpGrid Constructor https://phpgrid.com/documentation/construct-our-phpgrid/ Tue, 14 Sep 2010 07:47:05 +0000 http://64.38.236.7/?p=338 After successful installation, call the constructor like the following: 12345// always add namespace and include conf.php on top of every script use phpCtrl\C_DataGrid; require_once('/path/to/conf.php'); $dg = new C_DataGrid("SELECT * FROM Orders", "orderNumber", "Orders"); The details of phpGrid constructor are explained below: __constructor($sql, $sql_key=’id’, $sql_table=”, $db_connection=”) Parameter(s): $sql: SQL SELECT statement. This is the only SQL […]

The post phpGrid Constructor appeared first on phpGrid - PHP Datagrid.

]]>
After successful installation, call the constructor like the following:

1
2
3
4
5
// always add namespace and include conf.php on top of every script
use phpCtrl\C_DataGrid;
require_once('/path/to/conf.php');

$dg = new C_DataGrid("SELECT * FROM Orders", "orderNumber", "Orders");

The details of phpGrid constructor are explained below:

__constructor($sql, $sql_key=’id’, $sql_table=”, $db_connection=”)

  • Parameter(s):
    • $sql: SQL SELECT statement. This is the only SQL statement users’ needs to implement. The SELECT statement must include the primary key as one of the columns if not using the wildcard start (*) in SELECT statement.
      • This parameter can also be a local array (version 5.5+). Refer to phpGrid local array data source demo.
      • This parameter can also be the name of a stored procedure (version 7.5).  In that case, the 2nd parameter is an array containing parameters value.
    • $sql_key: The name of the database table primary key. The default is “id”.
      • Starting version 6, phpGrid supports composite primary key (Requires commercial licenses) by simply passing an array of string as $sql_key.
      • Note that composite PK is not supported as foreign key referential in master/detail and subgrid.
    • $sql_table: Name of the database table used in the SQL statement. If not defined, the table name is automatically parsed from the SQL statement.
    • $db_connection: Optional. Since version 4.3, the fourth parameter was introduced and used to overwrite connection parameters in conf.php. It allows datagrid to reference to a different data source on the fly. See Reference to Multiple Databases example for more information on this parameter. If you have only one database, you can safely ignore this parameter.
  • Description:
    • This is our datagrid object constructor. Use this method to create the phpGrid object. Usually, this is the first line in your code.
  • Example:
Do NOT include WHERE clause in $sql. Instead use set_query_filter() method (version 4.2+) to set query filter. If not using the wildcard star(*), the SELECT statement MUST include the primary key as one of the columns.
X

Single primary key:

1
$dg = new C_DataGrid("SELECT * FROM Orders", "orderNumber", 'Orders');

– OR –

1
$dg = new C_DataGrid("SELECT * FROM Orders", array("orderNumber"), 'Orders');

Composite primary key (commercial licenses only)

1
$dg = new C_DataGrid("SELECT * FROM orderdetails", array("productCode", "orderNumber"), "orderdetails");

Check out the Basic Datagrid example and composite primary key online example.

The post phpGrid Constructor appeared first on phpGrid - PHP Datagrid.

]]>
338