PHP: Create a date/ timestamp with strtotime by setting the time

Code sur écran d'ordinateur

Everyone knows the strtotime function. With it, for example, we can retrieve the date of 2 weeks ago, strtotime(‘-2 week’); or a year ago strtotime(‘-1 year’); What is interesting is that you can retrieve the date of 2 weeks ago while defining the time, for example midnight strtotime(‘-2 week midnight’); or noon: strtotime(‘-2 week noon’); … Read more

Categories PHP

PHPUnit – Set a protected or private property on a mock

MacBook avec code sur un bureau

Using mocks it is often necessary to give a value to a private or protected variable. To do this, just use the Reflection class. $basket = $this->getMockBuilder(‘Basket’) ->disableOriginalConstructor() ->getMock(); $basketReflection = new ReflectionClass($basket); $sessionId = $basketReflection->getProperty(‘sessionId’); $sessionId->setAccessible(true); $sessionId->setValue($basket, ‘abc’); So the sessionId value in our Mock will be ‘abc’. Since PHP 8.1, setAccessible(true) is unnecessary … Read more

Yii – Create a custom relation between two tables

MacBook avec code sur un bureau

To make a custom relationship between two tables in a model, it is possible to add an additional option in the relation in which one indicates which foreign key should be used with which field. public function relations() { return array( ‘nomRelation’ => array( self::HAS_ONE, ‘otherTable, ”, ‘on’=>’thisTable.fk_otherTable = otherTable.id’ ), ); }

Categories yii

PHPUnit – How to mock an object and its methods

Code sur écran d'ordinateur

When you start using PHPUnit, you often find yourself facing the need to simulate a method or object. Fortunately PHPUnit has some methods that can be very useful. How to simulate an object The easiest way to create a mocked object $product = $this->getMockBuilder(‘Product’) ->getMock(); or $product = $this->getMockBuilder(‘Product’) ->setMethods(array()) ->getMock(); This product is a … Read more

PHP – DOMDocument and XPath to parse a large XML file

Code sur écran d'ordinateur

When we have to process a very large XML, it is unfortunately not possible to use SimpleXML. Fortunately for this, we can use DOMDocument. To do this, just load the desired file and initialize xpath on it with DOMXPath. $document = new DOMDocument(); $document->load($file); $xpath = new DOMXPath($document); If the XML in question has a … Read more

Categories PHP

PHP and SimpleXML

Code sur écran d'ordinateur

When you want to process an XML file in PHP, there are two possibilities: either we use SimpleXML or we go through DOMDocument. In this article I will deal more specifically with SimpleXML. To load a file, just use the simplexml_load_file method $xml = simplexml_load_file($file); By doing so, you get a SimpleXMLElement. If the XML … Read more

Categories PHP

PHP – How to load a FILE in PHP from a form

Code sur écran d'ordinateur

To find out how to load a file in PHP from a form, you have to start by formatting the form correctly. It is mandatory to use the post method as well as the multipart/form-data enctype, otherwise PHP will not receive the necessary data. <form action=”/” method=”post” enctype=”multipart/form-data”> <input type=”file” name=”namefile” /> <input type=”submit” value=”ok” … Read more

Categories PHP