پیشنهاد فرادرس

کار با Databases در PHP شی گرا

دسته بندی ها: آموزش پی اچ پی (PHP) ، برنامه نویسی شی گرا (OOP) ، آموزش طراحی وب ، آموزش های لیندا (Lynda)

کار-با-databases-در-php-شی-گرا

تقریبا می توان گفت 90 درصد وب سایت ها نیاز به پایگاه داده دارند. زبان PHP راهکارهای زیادی برای ارتباط با انواع پایگاه داده دارد اما اکثرا از MYSQL استفاده می کنند.

این دوره آموزشی محصول Lynda است و نحوه ارتباط با پایگاه داده در PHP به صورت شی گرا را آموزش می دهد.

سرفصل های دوره آموزشی:

  • معرفی دوره
  • برنامه نویسی شیء گرا
  • Object چیست؟
  • Class چیست؟
  • تاریخچه object-oriented PHP
  • تعریف یک کلاس
  • استفاده از کلمه کلیدی static
  • دسترسی به کلاس های بدون Instantiation
  • پیاده سازی روش های استاتیک
  • ایجاد یک کلاس پایگاه داده
  • کار با object های Referencing
  • پیاده سازی یک الگوی استراتژی
  • نتیجه گیری

لیست سرفصل های دوره آموزشی:

Often it's optimal to only connect to the database once. I could connect and disconnect every time I needed to query the database, but that's hugely inefficient. I could use a global variable assigned to the connection, but that's fragile at best, as anything could manipulate it. It's better to control access, and restrict the number of connections. I am going to demonstrate how to create a simple, but still useful class that will only allow one connection to the database at one time. It'll provide access throughout the application, and minimize the possibility for corruption.

To do that, I'm going to use the object- oriented database connection from the MySQLi improved extension (or MySQLi). Create a new file in your Exercise folder, and call it class_Database.inc. Start a new class definition for the database. And, add some documentation. Only one connection is allowed.

The first property to create will contain the connection. Make it private, as the class will be controlling access to it. Next, make a private static property called instance. Remember, static properties can be changed after being defined, and the value is stored at the class level. private static $_instance. Provide a public static method to allow any part of the program to access the database instance.

public static function getInstance. If the instance does not exist, create it using self, only as a function call. if not self instance, self ::$_instance = new self. Then, return the static instance. return self::$_instance. Add proper documentation, get an instance of the database, and return type database.

Now, define the constructor for the database object. public function __construct. Start the constructor with a new MySQLi connection. The arguments are in the following order: host, username, password, and database name. As your environment may be different from mine, take note of your connection details, and replace as necessary. Error handling, if mysqli_connect_ error, then trigger error (failed to connect to MySQL, followed by my mysqli_connect_ error(), and we'll set the level as E_USER_ERROR;.

I want to prevent the database connection from getting copied, so I'll use the magic method clone, which specifies how an object is duplicated. I'll get into object cloning a bit later on, but for now, I'll just define an empty clone magic method, which will do nothing, including no copying, which is exactly what I want. Empty clone magic method to prevent duplication, private function __clone, and two curly braces ({}).

Finally, define a public method for getting the MySQLi connection. Get the MySQLi connection. Public function getConnection. return $this->_connection. Save the file. Now that a database connection is available, let's go back to the Address class, and get rid of the placeholder postal code lookup. Navigate to _postal_code_ guess, and replace the contents.

First, get an instance of the database. Next, get the connection. Write the SQL query. You'll want to get the postal code from the location table on the database, which is provided in the Exercise Files. SELECT postal_code from location.

Escape the variables, as the biggest danger to your application is user input. city_name is mysqli->real_escape_string ($ this->city_name); WHERE city_name = $city_name. We'll do the same for the subdivision name. $this->subdivision_name; AND subdivision_name = execute the query that you have created. $result = mysqli->query($sql_query).

If there was a match, we turn the postal code if ($row = $result->fetch_associative array, return $row 'postal code'. Save the Address class, then return to the demo file. As we've created a new database class, required at the top. require 'class.Database.inc. This file has gotten a bit long with all the testing, so let's simplify a bit.

Remove the empty address. Next, unset the postal code in the first address to test the new lookup. Then, replace the var export, and display after setting the address properties with an echo address, using the two-string magic method. Remove the Testing magic __get and __set. In the second address, remove the postal code, then replace the explicit call to display with echo $address_2. We don't need to debug the address types anymore, so they can be safely removed.

Save the demo, then return to the browser, and refresh. I can see that the postal codes are still being displayed, but now they're coming from the database. In this chapter, we accessed classes without instantiation. First, I discussed how to use the static keyword. I used the scope resolution operator to access static properties and methods. I demonstrated how to set constant values in the class, which never change, then implemented a static method to validate address type IDs. Finally, I created the database class to only allow one connection at a time.

With this foundation and ability to control access to resources, you have the beginnings of a stable and robust system, logically-structured and named. In the next chapter, I'll add further structure and hierarchy using class relationships.

Introduction 4m 10s Welcome 39s Exercise files 2m 16s What you should know 1m 15s 1. Introduction to Object-Oriented Programming 7m 47s What is an object? 2m 6s What is a class? 2m 7s Why should you use object-oriented programming? 2m 14s The history of object-oriented PHP 1m 20s 2. Creating Your First Object 16m 40s Defining a class 2m 58s Defining class properties 3m 26s Creating a method and exploring object context with $this 2m 50s Instantiating an object and accessing its contents 3m 19s Specifying the visibility scope 4m 7s 3. Overloading with Magic Methods 15m 51s What is a magic method, and do I need one? 2m 23s Overloading property access 6m 37s Customizing object construction 4m 34s Standardizing object rendering as a string 2m 17s 4. Accessing Classes without Instantiation 20m 54s Using the static keyword 3m 36s Leveraging scope resolution operators 1m 10s Setting constant values 2m 47s Implementing static methods 5m 43s Creating a database class 7m 38s 5. Class Relationships and Interactions 26m 19s Extending your class 6m 8s Abstracting classes 5m 57s Sharing interfaces using polymorphism 4m 39s Overriding methods, properties, and constants 3m 25s Cloning and comparing objects 2m 51s Referencing objects 3m 19s 6. Built-In PHP Objects 14m 52s Leveraging standard class objects 2m 42s Retrieving objects from the database 5m 41s Error handling with exceptions 2m 6s Customizing PHP exceptions 4m 23s 7. Design Patterns 8m 2s Identifying the singleton pattern 1m 42s Using the factory method pattern 1m 51s Implementing a strategy pattern 4m 29s Conclusion 2m 57s Looking forward to namespaces 47s Next steps 1m 6s Goodbye 1m 4s

عنوان دوره: Lynda - Accessing Databases with Object-Oriented PHP حجم فایل: 850MB

Lynda Accessing Databases with Object-Oriented PHP

پیشنهاد فرادرس