LESS初体验(一)

less是什么

LESS是一种由Alexis Sellier设计的动态层叠样式表语言,受Sass所影响,同时也影响了Sass的新语法:SCSS。

LESS是开源的,其第一个版本由Ruby写成,但在后续的版本当中,Ruby逐渐被替换为JavaScript。受益于JavaScript,LESS可以在客户端上运行(IE6+、Webkit、Firefox),也可以在服务端运行(Node.js、Rhino)。

在语法方面,LESS与CSS较为接近,一个合法的CSS代码段本身也是一段合法的LESS代码段。LESS提供变量、嵌套、混合、操作符、函数等一般编程所需的抽象机制

Less之于Css相当于Jquery至于JavaScript

编译工具

  • koala
  • Nodejs库
  • 浏览器端使用

使用koala

下载地址:http://koala-app.com/index-zh.html
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Less demo01</title>
    <link rel="stylesheet" href="style/styles.css">
</head>
<body>
 hello less!!   
</body>
</html>

styles.less

@charset "utf-8";
@title-size:40px;

body {
    background-color:red;
    font-size:@title-size;
}

koala自动编译styles.css

ThinkPHP快速搭建MVC

分步骤:
1. 创建Model
2. 创建View
3. 创建Controller
4. 修改配置config.php

1. 创建Model

在目录Home\Model创建文件UserModel.class.php

<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model {
    protected $trueTableName = 'wp_users'; 
}

2. 创建View

在目录Home\View\Test创建文件test.html

this is template!!
<br>
param:{$name}
<br> 
<?php echo($name);?>
<?php
    //echo phpinfo();
//echo phpversion();
?>

3. 创建Controller

在目录Home\Controller创建文件TestController.class.php

<?php
namespace Home\Controller;
use Think\Controller;

class TestController extends Controller {


    public function test() {
        //$this->show("this is testhaha",'utf-8');
        //$this->success('新增成功22', 'test2');

        $User = new \Home\Model\UserModel('users','wp_','DB_CONFIG');
        $list = $User->select();


        //$name = 'ThinkPHP';
        $this->assign('name',$list[0]['user_login']);
        //$this->display();
        $this->display('test');
    }
}

4. 修改配置

在目录Home\Conf修改文件config.php

<?php
return array(
    //'配置项'=>'配置值'
    'DB_CONFIG' => array(
         'db_type'  => 'mysql',
         'db_user'  => 'root',
         'db_pwd'   => '******',
         'db_host'  => '121.42.151.190',
         'db_port'  => '3306',
         'db_name'  => 'wordpress'
    ),
);

访问地址:http://localhost:8080/cms/index.php/Home/Test/test

浏览器显示如下:

    this is template!! 
    param:Rick
    Rick

PHP面向对象编程

AB是两个平级目录,A目录下有个UserModel.phpModel.php,UserModel 继承自Model.php;B目录下有个调用者index.php

Model.php

<?php
class Model {

   public function getMsg() {
        return "this parent message";
   }
}

UserModel.php

<?php
require 'Model.php';
class UserModel extends Model {

    private $name;

    public function __construct($name) {
        echo parent::getMsg().'<br>'; //调用父类方法
        $this->name = $name;
    }

    public function setName($name) {
        $this->name = $name;
    }

     public function getName() {
        return $this->name;
    }

    public static function getStaticFun() {
        echo "hello static";
    }
}

index.php

<?php

require "../UserModel.php";

$User = new UserModel('Leno.Li');
echo $User->getName().'<br>';

$User->setName('Rick.Xu');

echo $User->getName()."<br>";

UserModel::getStaticFun(); //调用类的静态方法
echo "<br>";
$User->getStaticFun();  //通过实例调用静态方法
//UserModel.getStaticFun(); error

Output

this parent message
Leno.Li
Rick.Xu
hello static
hello static

thinkphp初体验-基于MVC开发

官方手册请参考:ThinkPHP3.2.*版本开发手册

快速学习thinkphp从以下几个方面入手:
* 控制器
* 模型
* 视图
* 模板

从上述几个方面构建一个简单的基于MVC的实例:

1. 控制器

在目录Home\Controller\创建测试文件TestController.class.php

<?php
namespace Home\Controller;
use Think\Controller;

class TestController extends Controller {


    public function test() {
        //$this->show("this is testhaha",'utf-8');
        //$this->success('新增成功22', 'test2');

        $User = new \Home\Model\UserModel('users','wp_','DB_CONFIG');
        $list = $User->select();


        //$name = 'ThinkPHP';
        $this->assign('name',$list[0]['user_login']);
        //$this->display();
        $this->display('test');
    }
}

2. 模型

Home\Model下创建UserModel.class.php

<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model {
    //protected $tablePrefix = 'wp_';
    protected $trueTableName = 'wp_users'; 
}

3. 视图

Home\View\Test下创建test.html

this is template!!
<br>
param:{$name}
<br> 
<?php echo($name);?>

4. 模板

5.配置文件

Home\Conf\config.php

<?php
return array(
    //'配置项'=>'配置值'
    'DB_CONFIG' => array(
         'db_type'  => 'mysql',
         'db_user'  => 'root',
         'db_pwd'   => '123456',
         'db_host'  => '127.0.0.1',
         'db_port'  => '3306',
         'db_name'  => 'db'
    ),
);

访问地址: http://localhost:8080/cms/index.php/Home/Test/test

这里必须强调一下命名规范,不然会有很多的坑。
* 类文件都是以.class.php为后缀(这里是指的ThinkPHP内部使用的类库文件,不代表外部加载的类库文件),使用驼峰法命名,并且首字母大写,例如 DbMysql.class.php;
* 类的命名空间地址和所在的路径地址一致,例如Home\Controller\UserController类所在的路径应该是 Application/Home/Controller/UserController.class.php
* 确保文件的命名和调用大小写一致,是由于在类Unix系统上面,对大小写是敏感的(而ThinkPHP在调试模式下面,即使在Windows平台也会严格检查大小写);
* 类名和文件名一致(包括上面说的大小写一致),例如 UserController类的文件命名是UserController.class.phpInfoModel类的文件名是InfoModel.class.php, 并且不同的类库的类命名有一定的规范;
* 函数、配置文件等其他类库文件之外的一般是以.php为后缀(第三方引入的不做要求);
* 函数的命名使用小写字母和下划线的方式,例如 get_client_ip;
方法的命名使用驼峰法,并且首字母小写或者使用下划线“”,例如 getUserName,_parseType,通常下划线开头的方法属于私有方法;
* 属性的命名使用驼峰法,并且首字母小写或者使用下划线“
”,例如 tableName、_instance,通常下划线开头的属性属于私有属性;
* 以双下划线打头的函数或方法作为魔法方法,例如 __call 和 __autoload;
* 常量以大写字母和下划线命名,例如 HAS_ONE和 MANY_TO_MANY;
* 配置参数以大写字母和下划线命名,例如HTML_CACHE_ON;
* 语言变量以大写字母和下划线命名,例如MY_LANG,以下划线打头的语言变量通常用于系统语言变量,例如 _CLASS_NOT_EXIST_;
* 对变量的命名没有强制的规范,可以根据团队规范来进行;
* ThinkPHP的模板文件默认是以.html 为后缀(可以通过配置修改);
* 数据表和字段采用小写加下划线方式命名,并注意字段名不要以下划线开头,例如 think_user 表和 user_name字段是正确写法,类似 _username 这样的数据表字段可能会被过滤。

PHP操作Mysql

省略php的安装配置,以及如何配置对mysql的支持。

操作代码如下:

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "123456";
$mysql_database = "db";


$sql = "SELECT id,user_login,user_email FROM wp_users";


// 创建连接
$conn = new mysqli($servername, $username, $password, $mysql_database);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出每行数据
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". $row["id"]. " - Name: ". $row["user_login"]. " " . $row["user_email"];
    }
} else {
    echo "0 个结果";
}
$conn->close();
?>