Using property files in php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

PHP properties implementation

License

SerafimArts/Properties

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Scrutinizer CI

PHP Properties implementations based on getters or setters method and used PSR-5 PHPDoc information.

composer require serafim/properties

Properties provide the ability to control the behavior of setting and retrieving data from an object fields.

There are no properties at the language level, but they can be implemented with the some additional functionality.

class MyClass < protected $a = 23; public function __get($field) < return $this->$field; > > $dto = new MyClass(); echo $dto->a; // 23 $dto->a = 42; // Cannot access protected property MyClass::$a

This code example does not provide type-hint and autocomplete. Using magic without the ability to control it is always a bad option. . well, it looks awful %)

We can fix some problems using PSR-5. In this case, we can add a docblock.

/** * @property-read int $a */ class MyClass < // . 

But this docblock only adds information for the IDE and does not affect the code itself. At the same time, you should always keep it up to date.

But wait! We have another problem.

$dto = new MyClass(); echo isset($dto->a); // false

This is obviously a bug. We still need to add __isset , __unset and __set support.

Now let’s see what the serafim/properties package provides.

/** * @property $a */ class MyClass < use Serafim\Properties\Properties; protected $a = 23; > $dto = new MyClass(); $dto->a; // 23 $dto->a = 42; // Ok $dto->a; // 42

To indicate that the type should be readonly use @property-read annotation.

/** * @property-read $a */ class MyClass < use Serafim\Properties\Properties; protected $a = 23; > $dto = new MyClass(); $dto->a; // 23 $dto->a = 42; // Error: Property MyClass::$a is readonly

To indicate that the type should be readonly use @property-write annotation.

/** * @property-write $a */ class MyClass < use Serafim\Properties\Properties; protected $a = 23; > $dto = new MyClass(); $dto->a = 42; // 42 $dto->a; // Error: Property MyClass::$a is writeonly

For getter or setter override just declare get[Property] ( is[Property] for bool values will be works too) or set[Property] methods.

/** * @property-read int $a */ class MyClass < use Serafim\Properties\Properties; protected $a = 23; protected function getA() < return $this->a + 19; > > $dto = new MyClass(); echo $dto->a; // 42 (because 23 + 19 = 42) $dto->a = 42; // Error: Property is read-only (@property-read doc declaration)
/** * @property-write string $anotherProperty */ class Some < // . protected $anotherProperty = 'some'; /** * @param string $newVal */ public function setAnotherProperty($newVal) < // Just example if (mb_strlen($newVal) > 4) < throw new InvalidArgumentException('. '); > $this->anotherProperty = $newVal; > >

All these annotations fully work in the IDE, including autocomplete and highlighting incorrect behavior.

IDE Autocomplete And Highlighting

All properties with writeable behavior will be «type checkable».

/** * @property int|null $a */ class Some < use Serafim\Properties\Properties; protected $a; > // $some = new Some; $some->a = 23; // Ok $some->a = null; // Ok $some->a = 'string'; // Error: "TypeError: Value for property Some::$a must be of the type int|null, string given"
  • int (or integer ) — property value are integer
  • bool (or boolean ) — value are boolean
  • float (or double ) — value are float
  • string — value are string or object with __toString method
  • null (or void ) — value are nullable
  • resource — value are resource
  • object — value can be any object
  • mixed — no type checking
  • callable — value can be string, instance of \Closure, array with 2 args or object with __invoke method
  • scalar — value cannot be an object
  • countable — value can be a countable (array or object provided Countable interface).
  • self — value can be object of self class or string with name of self class

self keyword does not available yet: it will be supports in future

static keyword does not available yet: it will be supports in future

$this keyword does not available yet: it will be supports in future

  • array — value is type of array
  • Class[] — value is type of array or instance of \Traversable
  • scalar[] — value is type of array or instance of \Traversable
  • Collection<> — value is type of array or instance of «Collection» and \Traversable
  • Collection — value is type of array or instance of «Collection» and \Traversable
  • Collection — value is type of array or instance of «Collection» and \Traversable

Conjunction And Disjunction

  • a|b — means that the value must be type (a or b) .
  • a&b — means that the value must be type (a and b) .
  • a|b&c — means that the value must be type (a or (b and c)) .

The code is quite effective, but in the production mode you should use caching. The package implements support for the PSR-16 standard.

$driver = new Psr16CacheDriver(); // Your PSR16 cache driver implementation $properties = Serafim\Properties\Bootstrap::getInstance(); $properties->setCacheDriver($driver);

About

PHP properties implementation

Источник

Road To Automation

For reading and writing data into properties file I am using parse_ini_file() function in this post.
Create a file name with extension .properties like I have created below:

If you run above file you will get Name and Url values.

Writing Data: below code write data into mentioned file if key is already exist then it update value and if key not exist it add new key value. Also if file not exist then it create new file.

1 comment:

You always provide quality based posts, enjoy reading your work. html5 video scene selection Reply Delete

Leave your comments, queries, suggestion I will try to provide solution

Search This Blog

Followers

Facebook Badge

Blog Archive

Total Pageviews

Labels

Recent Comments

For reading and writing data into properties file I am using Properties class in this post. Create a file name with extension .properties .

Sauce lab provided screen capturing functionality to test scripts execution, I was looking same type functionality to work on local machine.

Perquisite: Download and install xampp application into your machine from link: «http://www.apachefriends.org/en/xampp-windows.html&.

SoapUI Pro has a feature to read data from external files like: excel, csv etc. But SoapUI does not provide such feature to read data from .

In this post I will show you how to automate HTML5 video. To automate this we will use java scripts function to control video. So here we .

About Appium: Appium is open source automation tool for iOS and android application supported by Sauce Lab. It is cross platform, supporte.

In my previous post «» Road to create test suite for python Webdriver scripts , I posted that how to create Suite file for webdri.

Prerequisite: 1. Download and install Sikuli from for more information visit link «http://www.sikuli.org/download.html 2. D.

For reading and writing data into properties file I am using parse_ini_file() function in this post. Create a file name with extension .pr.

In this post I will show you how to execute SoapUI test for a set of values. For this I have put all data in csv file. I write groovy scri.

Источник

Can we have PROPERTIES file in PHP like JAVA?

send pies

posted 11 years ago

  • Report post to moderator
  • I want to know can we have .PROPERTIES file as like JAVA in PHP to access resources?
    if yes then tell me how is it?

    Aniket Kulkarni
    Oracle Certified Professional, Java SE 6 Programmer.

    send pies

    posted 11 years ago

  • Report post to moderator
  • yes you can use properties file but it doesn’t behave like in java, let say you have file like this.

    value1 =text one
    value2 =text two
    value3 =text three

    so when you load this file like this.

    this returns you the array like this

    while php has another way of loading key value file using parse_ini_file check this out.

    send pies

    posted 11 years ago

    • 1
  • Report post to moderator
  • Java uses properties files because Java code gets compiled into binary format and therefore making any code changes becomes difficult. By removing configuration values ot a text file, Java enables people to change the confioguration without recompiling.

    PHP, on the other hand, is not compiled into binary. Therefore, the PHP files are always editable by a a plain text editor. Most PHP apps handle confguration options by placing them into a separate PHP file with lots of comments describing each setting. And the settings are directly made to variables. Then any PHP code that needs those settings includes that file and then uses the variables. For an example of this, look at the settings.php file that comes with Drupal.

    Источник

    Читайте также:  Python случайное число float
    Оцените статью