I'd like to ask about the concept of php OOP and sort out some parts that I don't understand.

I have recently seen a lot of OOP teaching mysqli (or PDO), found it to be a new world). The way I now connect to databases and statements is to introduce a large php file on each page, including links, and there are many small php files, which may be functions, or mysql statements, and use switches to set which page will be used. Until I got to know OOP, I found that there are many programs to optimize, but I have a lot of problems in mind.
I want to replace the old writing with a new one. I just don"t know where to start, so there seems to be a lot of improvement.
, including my css,js, uses a php file to introduce
, so there will be at least two introductions on each page. One may be header.php and the other is footer.php
, and there are a lot of php in it. This at least ensures that every page is Synchronize accessible, but I"m not sure it"s right. The functions that are useful are reintroduced, and those that are not used are not introduced.? . Wait

  1. what is the concept that php is separated from html?
  2. I do not currently use the framework, so now the way the site is written is old, due to time pressure, it is impossible to get rid of retraining, but at least I want to slowly change the logic of the framework to the current site, the first thing I think of is to connect the database interaction, in the past I was the above declaration statement, the following is while ($row = mysqli_fetch. The method of writing to fish out the data will almost be accompanied by html, rather than pure json format. How can this method be incorporated into the concept of OOP and improved?
  3. I have watched a lot of OOP film teaching, and everyone"s OOP mysqli writing is almost different. What I want to make sure is how do you write it? I just want to find the safest way to write it. (my goal is to make my code accessible to others.)
  4. useful class functions are re-introduced, not used will not be introduced, so I found the concept of namespace, but I still do not understand this movement logic, even if I have read the real-time coding, but do not know how to apply to the current old writing, I do not know if there is a great recommendation or experience to replace the writing?

I find that there are more functions, and it is not right for each page to introduce features that are not needed to think about it. The more you switch.. It is getting uglier and uglier. So I decided to send an article to ask about the experience of the great gods. Thank you for sharing. Thank you very much

Nov.10,2021

in addition, if you like, you can learn about the project "the way of PHP" and a book written by its sponsor, "Modern PHP", which is not expensive, so it is worth reading.

< hr >

8102 years ago, if you are not doing command-line applications, you are not unaware of the separation of PHP and HTML.
then you have basically learned this PHP for nothing.

learn to use the framework as soon as possible, and you will open the door to a new world

everyone has their own programming style, and it is now recommended to use PDO instead of MySQLi directly.

8102 years old, you have to learn to use the modern package management tool (composer) to manage your package. You should be fully aware of PSR 1-2-4. 3 recommended standards.


  1. PDO: is actually an encapsulation of an operation. When you use php to connect to mysql, nosql these databases, you have to check your own api, and spell sql statements one by one, including escape, Filter and other security things. On the other hand, pdo abstracts the interfaces of all databases, so you don't have to contact the database directly, and it also does most security operations, so the coding intensity drops sharply. Of course, you still need to know something about the database. Then if you want to change the original code to pdo, it depends on the level of your previous code, if the design is good, there may be a few functions, if the design is not good, each page needs to be changed, you have to measure the ratio of costs and benefits. If it were me, I'd rather push it all over again.
  2. template engine (separation of php and html): actually, there is no separation. It also encapsulates the specific operation. To put it simply, for example, if you have a variable of $xxx, and then a {$xxx} is written in the html file, it will read the contents of the corresponding html file and change all {$xxx} into the value of $xxx. Of course, the details must be more complicated than this. Specifically, you can understand the idea of MVC
  3. .
  4. Namespace: I specifically looked up the information. The concept of namespace has been around since php5.3. The explanation of this concept is very long. Simply understand that you think of it as a directory implemented by php, that is, to introduce a file, you may need to find it according to the relative path or absolute path, but now the path has been replaced with a namespace
  5. .
< H2 > suggestions < / H2 >
  1. read some books, such as "the way of php" recommended upstairs
  2. take a look at the official document of php h http://php.net/manual/zh/. Most of it is in Chinese, and a small part of English is not difficult
  3. .
  4. you can look for the PDO or template engine you want on composer. There are good solutions and should also be available in Chinese. PDO generally I use whatever is built into the framework, and I have written some simple ones, but it is difficult to get into the elegant hall, so I cannot recommend them to you. Template engine Smarty , Twig are all good, and Twig is recommended.
  5. it doesn't matter if you practice every day, but you'd better use the framework to write a project. It will save a lot of trouble, and it will also be beneficial to code inheritance
  6. .
< H1 > superficial opinions. If there are any mistakes, please correct them < / H1 >.

1, in fact, there is no concept of separation of PHP and HTML, in the view layer you can also use PHP+HTML to write, to separate view and business logic. You can simply separate the view file from your business logic with the output control function + extract .

  

the separated template seems to be written separately, but in fact, the specific identity is parsed by the template engine!
you can take a look at Smarty php. Most of them write services or interfaces that don't handle things about views! Get rid of small websites.
if you really want to know about template engine, find a video. It's not difficult to replace
with regular matching. As for PDO, it encapsulates the operation of the database

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be5924-31784.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be5924-31784.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?