Hoi,
1.5 jaar na mijn herseninfarct probeer ik PHP weer op te pakken.
Beetje bij beetje probeer ik mijn eigen cms (MVC model) weer wat op te frissen maar ik loop nu tegen iets aan waar ik even geen antwoord op heb.
Ik heb dit stuk code
Dit wordt aangeroepen vanuit een andere classe settings hij haalt de settings op van het cms in een jason stringPHP Code:
public static function find($id, $format=false) {
$q = "SELECT * FROM `" . static::TABLE . "` WHERE `" . static::$primaryKey . "` = :id" . (static::DELETED === true ? " AND deleted_at IS NULL" : null);
$stmt = self::$db->prepare($q);
$stmt->bindParam('id', $id, PDO::PARAM_INT);
$stmt->execute();
$object = $stmt->fetchObject(get_called_class());
return ($format === false ? $object : self::toArray($object));
}
Dit is de mooie foutmelding die ik krijgPHP Code:
$settings = self::find(1);
self::$data = json_decode($settings->settings);
Uncaught exception: 'ErrorException'
Message: 'Creation of dynamic property App\Models\Settings::$id is deprecated'
Stack trace:
#0 C:\xampp\htdocs\cms\App\Core\Model.php(29): App\Core\Error::errorHandler(8192, 'Creation of dyn...', 'C:\\xampp\\htdocs...', 29)
#1 [internal function]: App\Core\Model->__set('id', 1)
#2 C:\xampp\htdocs\cms\App\Core\Model.php(52): PDOStatement->fetchObject('App\\Models\\Sett...')
#3 C:\xampp\htdocs\cms\App\Models\Settings.php(23): App\Core\Model::find(1)
#4 C:\xampp\htdocs\cms\App\Core\Translation.php(98): App\Models\Settings::get('site_language_d...')
#5 C:\xampp\htdocs\cms\App\Core\Translation.php(114): App\Core\Translation->setLanguageId()
#6 C:\xampp\htdocs\cms\public_html\index.php(6): App\Core\Translation->loadLanguages()
#7 {main}Thrown in 'C:\xampp\htdocs\cms\App\Core\Model.php' on line 29
Line 29:
Ik zie even door de bomen het bos niet meer wat doe ik fout?PHP Code:
public function __set($name, $value) {
$this->$name = $value; // line 29
}
Bedankt voor de hulp!
Martijn
- PHP Creation of dynamic property is deprecated'
-
23-04-2025, 13:08 #1
- Berichten
- 501
- Lid sinds
- 18 Jaar
PHP Creation of dynamic property is deprecated'
-
In de schijnwerper
Te Koop Lastminute website met potentie en inkomsten.Website te koopLinkbuilding uitbesteden - 25 jaar ervaring - Zie behaalde resultatenAdvertentieruimteSenior Designster - top kwaliteit - 16 uur per week - per maand - 350 euroFreelance / Werk!!!!! Uniek portfolio met 515 .NL domeinnamen !!!!!Domein te koop -
23-04-2025, 15:25 #2
- Berichten
- 5.661
- Lid sinds
- 18 Jaar
Re: PHP Creation of dynamic property is deprecated'
Ik ben geen programmeur, maar het "is deprecated" klinkt als een functie die je niet in deze (nieuwere?) php-versie kunt gebruiken.
-
23-04-2025, 17:42 #3
- Berichten
- 537
- Lid sinds
- 17 Jaar
Re: PHP Creation of dynamic property is deprecated'
Geef het aan chatgpt, die heeft vast een antwoord. En krijg je direct.
-
23-04-2025, 19:05 #4
- Berichten
- 1.307
- Lid sinds
- 16 Jaar
Re: PHP Creation of dynamic property is deprecated'
Volgens QwQ-32B:
The error occurs because you're trying to dynamically create properties on your `Settings` model without defining them explicitly. Here's the breakdown and solution: ### Problem Analysis: 1. **Dynamic Properties Deprecation**: - When you call `fetchObject(get_called_class())`, PDO tries to map database columns to object properties. - If your `Settings` class doesn't have a declared `$id` property, PHP tries to dynamically create it. - Your `Model::__set()` method allows this, but PHP now deprecates dynamic property creation (PHP 8+). 2. **Root Cause**: - The `Settings` model lacks explicit property declarations for columns like `id`, `settings`, etc. - The `__set()` magic method in the parent `Model` class is being used to set these properties dynamically, which is now deprecated. --- ### Solution: **Option 1: Declare Properties Explicitly** (Recommended) Add public properties to your `Settings` class for all database columns it uses: ```php // In App\Models\Settings.php class Settings extends Model { public $id; // Primary key public $settings; // Your JSON settings column // Add other columns here... } ``` This tells PHP the properties exist upfront, avoiding the need for dynamic creation. --- **Option 2: Remove the `__set()` Method** (If you don't need it) If you don't need custom property handling, remove the `__set()` method in `Model` to let PHP handle the deprecation properly. This will force you to define all properties explicitly. ```php // Remove this from App\Core\Model.php public function __set($name, $value) { $this->$name = $value; // Remove this } ``` --- **Option 3: Silence the Warning** (Not recommended, but quick fix) Suppress the deprecation notice (not ideal, but works temporarily): ```php public function __set($name, $value) { @($this->$name = $value); // Add @ to suppress notices } ``` --- ### Best Practice: Always declare all database-mapped properties in your model classes. This improves code readability and avoids such issues. Example: ```php class Settings extends Model { public $id; public $settings; public $created_at; public $updated_at; // ... other columns } ``` This ensures PDO can map columns directly without needing dynamic properties.The error occurs because you're trying to dynamically create properties on your `Settings` model without defining them explicitly. Here's the breakdown and solution:
### Problem Analysis:
1. **Dynamic Properties Deprecation**:
- When you call `fetchObject(get_called_class())`, PDO tries to map database columns to object properties.
- If your `Settings` class doesn't have a declared `$id` property, PHP tries to dynamically create it.
- Your `Model::__set()` method allows this, but PHP now deprecates dynamic property creation (PHP 8+).
2. **Root Cause**:
- The `Settings` model lacks explicit property declarations for columns like `id`, `settings`, etc.
- The `__set()` magic method in the parent `Model` class is being used to set these properties dynamically, which is now deprecated.
---
### Solution:
**Option 1: Declare Properties Explicitly** (Recommended)
Add public properties to your `Settings` class for all database columns it uses:
```php
// In App\Models\Settings.php
class Settings extends Model {
public $id; // Primary key
public $settings; // Your JSON settings column
// Add other columns here...
}
```
This tells PHP the properties exist upfront, avoiding the need for dynamic creation.
---
**Option 2: Remove the `__set()` Method** (If you don't need it)
If you don't need custom property handling, remove the `__set()` method in `Model` to let PHP handle the deprecation properly. This will force you to define all properties explicitly.
```php
// Remove this from App\Core\Model.php
public function __set($name, $value) {
$this->$name = $value; // Remove this
}
```
---
**Option 3: Silence the Warning** (Not recommended, but quick fix)
Suppress the deprecation notice (not ideal, but works temporarily):
```php
public function __set($name, $value) {
@($this->$name = $value); // Add @ to suppress notices
}
```
---
### Best Practice:
Always declare all database-mapped properties in your model classes. This improves code readability and avoids such issues. Example:
```php
class Settings extends Model {
public $id;
public $settings;
public $created_at;
public $updated_at;
// ... other columns
}
```
This ensures PDO can map columns directly without needing dynamic properties.
Plaats een
- + Advertentie
- + Onderwerp
Marktplaats
Webmasterforum
- Websites algemeen
- Sitechecks
- Marketing
- Domeinen algemeen
- Waardebepaling
- CMS
- Wordpress
- Joomla
- Magento
- Google algemeen
- SEO
- Analytics
- Adsense
- Adwords
- HTML / XHTML
- CSS
- Programmeren
- PHP
- Javascript
- JQuery
- MySQL
- Ondernemen algemeen
- Belastingen
- Juridisch
- Grafisch ontwerp
- Hosting Algemeen
- Hardware Info
- Offtopic